Skip to content

Instantly share code, notes, and snippets.

@qqibrow
qqibrow / gist:4026927
Created November 6, 2012 19:32
leetcode: balanced binary tree
//copy other guy's code. This code check whether children satisfy the conditions
//and at the same time return the //their heights for the parent to check condition.
// http://blog.csdn.net/xshalk/article/details/8150030
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
@qqibrow
qqibrow / gist:4027251
Created November 6, 2012 20:20
leetcode: combine
// test error 1: should erase at this time not under the base condition
// item.push_back(i);
//DFS(i, n, k-1, item, results);
// item.erase( item.end() - 1);
class Solution {
public:
void DFS( int pos, int n, int k, vector<int>& item, vector<vector<int> > & results)
@qqibrow
qqibrow / gist:4027255
Created November 6, 2012 20:21
leetCode : addTwoNumber
// error1 : do not know how to init head :
// error2 : do not know the terminate condition should be more careful
// error3 : do not know how to go next
// error4 : do not know must check the carry at last may be add a new one
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode* p1 = l1, *p2 = l2;
@qqibrow
qqibrow / gist:4029604
Created November 7, 2012 04:43
leetCode: Anagram
// my code : a litter ugly. mainly because it's too long.
// ATTENTION: at first, there is a big problem. cause I use a 26 length bool array to statistic the characters.
//However, I falsely write feature[ str[i]] = true rather than feature[ str[i] - 'a' ] = true. then the program
//always have stack error, but not point out where is wrong. So next time must use vector.
// refer to other's code, there are 2 solutions.
// 1. compare the string after sort, but not reduce the complexity = sort + compare = O(n*mlgm) + (n^2*m)
// 2. use hash table. used the sorted string as the key, then anagrams will map to the same bucket. O( n* mlgm ). If we use linear time sort( cause they are strings), then it will be O(n*m) (m as the longest string)
@qqibrow
qqibrow / gist:4029614
Created November 7, 2012 04:44
leetCode: Anagram(hash)
// source: http://blog.csdn.net/xshalk/article/details/8149075
class Solution {
private:
map<string,vector<string> >m;
public:
vector<string> anagrams(vector<string> &strs) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
@qqibrow
qqibrow / gist:4029624
Created November 7, 2012 04:49
leetCode: 4Sum
class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main()
vector< vector<int> > results;
if(num.size() < 4)
return results;
sort( num.begin(), num.end());
@qqibrow
qqibrow / gist:4029653
Created November 7, 2012 05:06
leetCode:addBinary
class Solution {
public:
int char2int(char a)
{
if(a == '0')
return 0;
else return 1;
}
@qqibrow
qqibrow / gist:4029656
Created November 7, 2012 05:06
leetCode: 3SumClosest
class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
sort(num.begin(), num.end());
int sum;
int error = 0x0fffffff;
for( int i = 0; i < num.size() - 2; i++)
{
@qqibrow
qqibrow / gist:4030129
Created November 7, 2012 08:08
leetCode: maxProfit I
// use dynamic programming
// opt[i] means the largest profit when sell at time i
// I have 2 choices, to sell it or keep it
// opt[i] = max(0, opt[i - 1] + Prices[i] - Prices[i - 1])
class Solution {
public:
int maxProfit(vector<int> &prices) {
@qqibrow
qqibrow / gist:4030157
Created November 7, 2012 08:21
leetCode: maxProfit II
// just to sum up every increasing section
class Solution {
public:
int maxProfit(vector<int> &prices) {
if( prices.size() < 2)
return 0;
int profits = 0;