This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int char2int(char a) | |
{ | |
if(a == '0') | |
return 0; | |
else return 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// just to sum up every increasing section | |
class Solution { | |
public: | |
int maxProfit(vector<int> &prices) { | |
if( prices.size() < 2) | |
return 0; | |
int profits = 0; |
OlderNewer