锘??xml version="1.0" encoding="utf-8" standalone="yes"?>精品无码久久久久国产,国产精品久久久久久久久,色99久久久久高潮综合影院http://www.shnenglu.com/Uriel/category/20791.htmlResearch Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learningzh-cnFri, 28 Jul 2023 21:32:29 GMTFri, 28 Jul 2023 21:32:29 GMT60[LeetCode]Merge Intervals-2014.01.20http://www.shnenglu.com/Uriel/articles/205507.htmlUrielUrielMon, 20 Jan 2014 14:40:00 GMThttp://www.shnenglu.com/Uriel/articles/205507.htmlhttp://www.shnenglu.com/Uriel/comments/205507.htmlhttp://www.shnenglu.com/Uriel/articles/205507.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205507.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205507.html
 1 /**
 2  * Definition for an interval.
 3  * struct Interval {
 4  *     int start;
 5  *     int end;
 6  *     Interval() : start(0), end(0) {}
 7  *     Interval(int s, int e) : start(s), end(e) {}
 8  * };
 9  */
10  
11 struct seg {
12     int fg; //0->start
13     int val;
14 };
15 
16 bool cmp(seg a, seg b) {
17     if(a.val != b.val) return a.val < b.val;
18     return a.fg < b.fg;
19 }
20 
21 class Solution {
22 public:
23     seg s[100010];
24     vector<Interval> merge(vector<Interval> &intervals) {
25         int n = 0;
26         vector<Interval> res;
27         Interval tp;
28         if(!intervals.size()) return res;
29         for(int i = 0; i < intervals.size(); ++i) {
30             s[n].val = intervals[i].start;
31             s[n].fg = 0;
32             ++n;
33             s[n].val = intervals[i].end;
34             s[n].fg = 1;
35             ++n;
36         }
37         sort(s, s + n, cmp);
38         int nt = 0;
39         for(int i = 0; i < n; ++i) {
40             if(!s[i].fg) {
41                 nt++;
42                 if(nt == 1) tp.start = s[i].val;
43             }
44             else {
45                 nt--;
46                 if(!nt) {
47                     tp.end = s[i].val;
48                     res.push_back(tp);
49                 }
50             }
51         }
52         return res;
53     }
54 };


Uriel 2014-01-20 22:40 鍙戣〃璇勮
]]>
[LeetCode]Best Time to Buy and Sell Stock II-2014.01.20http://www.shnenglu.com/Uriel/articles/205506.htmlUrielUrielMon, 20 Jan 2014 13:51:00 GMThttp://www.shnenglu.com/Uriel/articles/205506.htmlhttp://www.shnenglu.com/Uriel/comments/205506.htmlhttp://www.shnenglu.com/Uriel/articles/205506.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205506.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205506.html
 1 class Solution {
 2 public:
 3     int maxProfit(vector<int> &prices) {
 4         int profit[100010], mx = 0, n = prices.size();
 5         for(int i = 0; i < n - 1; ++i) {
 6             profit[i + 1] = prices[i + 1] - prices[i];
 7         }
 8         for(int i = 1; i < n; ++i) {
 9             if(profit[i] > 0) mx += profit[i];
10         }
11         return mx;
12     }
13 };


Uriel 2014-01-20 21:51 鍙戣〃璇勮
]]>
[LeetCode]Interleaving String-2014.01.20http://www.shnenglu.com/Uriel/articles/205504.htmlUrielUrielMon, 20 Jan 2014 13:12:00 GMThttp://www.shnenglu.com/Uriel/articles/205504.htmlhttp://www.shnenglu.com/Uriel/comments/205504.htmlhttp://www.shnenglu.com/Uriel/articles/205504.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205504.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205504.html
sample錛?br />s1 = "aabcc",

s2 = "dbbca",

When s3 = "aadbbcbcac", return true.
When s3 = "aadbbbaccc", return false.

DP鍗沖彲錛宒p[i][j]琛ㄧず宸插尮閰峴3鍓峣涓瓧絎︼紝鍏朵腑j涓瓧絎︽潵鑷簬s1

 1 class Solution {
 2 public:
 3     int dp[1010][1010];
 4 
 5     bool isInterleave(string s1, string s2, string s3) {
 6         if(!s1.length()) return s2 == s3;
 7         if(!s2.length()) return s1 == s3;
 8         int len = s3.length();
 9         if(len != s1.length() + s2.length()) return false;
10         memset(dp, 0, sizeof(dp));
11         dp[0][0] = 1;
12         for(int i = 1; i <= len; ++i) {
13             for(int j = 0; j <= i && j <= s1.length(); ++j) {
14                 if(j >= 1 && s1[j - 1] == s3[i - 1] && dp[i - 1][j - 1]) {
15                     dp[i][j] = 1;
16                 }
17                 else if(j >= 0 && j < i && (i - j - 1) < s2.length() && s2[i - j - 1] == s3[i - 1] && dp[i - 1][j]) {
18                     dp[i][j] = 1;
19                 }
20                 if(i == len && dp[i][j]) return true;
21             }
22         }
23         return false;
24     }
25 };




Uriel 2014-01-20 21:12 鍙戣〃璇勮
]]>
[LeetCode]Best Time to Buy and Sell Stock & Maximum Subarray-2014.01.20http://www.shnenglu.com/Uriel/articles/205503.htmlUrielUrielMon, 20 Jan 2014 13:09:00 GMThttp://www.shnenglu.com/Uriel/articles/205503.htmlhttp://www.shnenglu.com/Uriel/comments/205503.htmlhttp://www.shnenglu.com/Uriel/articles/205503.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205503.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205503.htmlBest Time to Buy and Sell Stock緇欏嚭n澶╀腑姣忓ぉ鐨勮偂紲ㄥ競鍊鹼紝閫夊畾鍚堥傜殑鏃墮棿涔拌繘鍜屽崠鍑轟竴嬈★紝闂渶澶氳禋澶氬皯
涓寮濮嬫兂閮芥病鎯砄(n^2)鏆村姏涓嬈★紝鏋滅劧TLE
鐒跺悗鎯沖埌鑲$エ甯傚兼寜鏃ユ湡涓や袱鐩稿噺錛岀劧鍚庡氨鏄眰鏈澶у瓙孌靛拰鐨勯棶棰樹簡銆傘備笅闈袱縐嶅啓娉曡繖棰橀兘ok錛屼絾鍋歁aximum Subarray榪欓鐨勬椂鍊欏彂鐜扮浜岀鍐欐硶鏄敊鐨勶紝鍥犱負瀛樺湪鏁板垪鍏ㄨ礋鐨勬儏鍐碉紒

Maximum Subarray
瑁哥殑姹傛渶澶у瓙孌靛拰鐨勯棶棰橈紝璺熶笂棰樹竴鏍楓傘?br />
 1 class Solution {
 2 public:
 3     int maxProfit(vector<int> &prices) {
 4         int profit[100010], dp[100010], mx = 0, tp = 0, n = prices.size();
 5         for(int i = 0; i < n - 1; ++i) {
 6             profit[i + 1] = prices[i + 1] - prices[i];
 7         }
 8         dp[0] = 0;
 9         for(int i = 1; i < n; ++i) {
10             tp += profit[i];
11             if(tp > mx) {
12                 mx = tp;
13                 dp[i] = mx;
14             }
15             else
16                 dp[i] = dp[i - 1];
17             if(tp < 0) tp = 0;
18         }
19         return mx;
20     }
21 };

 1 class Solution {
 2 public:
 3     int maxProfit(vector<int> &prices) {
 4         int profit[100010], dp[100010], mx = 0, n = prices.size();
 5         for(int i = 0; i < n - 1; ++i) {
 6             profit[i + 1] = prices[i + 1] - prices[i];
 7         }
 8         dp[0] = 0;
 9         for(int i = 1; i < n; ++i) {
10             if(dp[i - 1] + profit[i] < 0) dp[i] = 0;
11             else
12                 dp[i] = dp[i - 1] + profit[i];
13             if(dp[i] > mx) mx = dp[i];
14         }
15         return mx;
16     }
17 };


Uriel 2014-01-20 21:09 鍙戣〃璇勮
]]>
[LeetCode]Binary Tree Maximum Path Sum-2014.01.19http://www.shnenglu.com/Uriel/articles/205482.htmlUrielUrielSun, 19 Jan 2014 13:48:00 GMThttp://www.shnenglu.com/Uriel/articles/205482.htmlhttp://www.shnenglu.com/Uriel/comments/205482.htmlhttp://www.shnenglu.com/Uriel/articles/205482.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205482.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205482.html
鏈潵鐪嬮鐩互涓烘槸鏍戝艦DP浠涔堢殑錛岀粨鏋滃張鏄疍FS銆傘傛病鑰冭檻璐熸暟鎯呭喌WA涓嬈°傘?br />
 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int res;
13 
14     int DFS(TreeNode *root, int sum) {
15         if(root == NULL) return INT_MIN;
16         int suml = DFS(root->left, sum);
17         int sumr = DFS(root->right, sum);
18         int tp = root->val;
19         if(suml > 0) tp += suml;
20         if(sumr > 0) tp += sumr;
21         res = max(tp, res);
22         return max(max(suml, sumr), 0) + root->val;
23     }
24     
25     int maxPathSum(TreeNode *root) {
26         if(root == NULL) return 0;
27         res = root->val;
28         DFS(root, root->val);
29         return res;
30     }
31 };


Uriel 2014-01-19 21:48 鍙戣〃璇勮
]]>
[LeetCode]Merge Sorted Array-2014.01.19http://www.shnenglu.com/Uriel/articles/205478.htmlUrielUrielSun, 19 Jan 2014 06:27:00 GMThttp://www.shnenglu.com/Uriel/articles/205478.htmlhttp://www.shnenglu.com/Uriel/comments/205478.htmlhttp://www.shnenglu.com/Uriel/articles/205478.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205478.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205478.html
 1 class Solution {
 2 public:
 3     void merge(int A[], int m, int B[], int n) {
 4         int pos = m + n - 1, p1 = m - 1, p2 = n - 1;
 5         for(int i = pos; i >= 0; --i) {
 6             if(p2 >=0 && (p1 < 0 || A[p1] < B[p2])) A[i] = B[p2--];
 7             else
 8                 A[i] = A[p1--];
 9         }
10     }
11 };


Uriel 2014-01-19 14:27 鍙戣〃璇勮
]]>
[LeetCode]Minimum Path Sum-2014.01.19http://www.shnenglu.com/Uriel/articles/205471.htmlUrielUrielSat, 18 Jan 2014 18:57:00 GMThttp://www.shnenglu.com/Uriel/articles/205471.htmlhttp://www.shnenglu.com/Uriel/comments/205471.htmlhttp://www.shnenglu.com/Uriel/articles/205471.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205471.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205471.html
 1 class Solution {
 2 public:
 3     int dp[1010][1010];
 4     int minPathSum(vector<vector<int> > &grid) {
 5         if(grid.empty()) return 0;
 6         for(int i = 0; i < grid.size(); ++i) {
 7             for(int j = 0; j < grid[0].size(); ++j) {
 8                 if(!i && !j) dp[i][j] = grid[i][j];
 9                 else if(!i) dp[i][j] = dp[i][j - 1] + grid[i][j];
10                 else if(!j) dp[i][j] = dp[i - 1][j] + grid[i][j];
11                 else
12                     dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
13             }
14         }
15         return dp[grid.size() - 1][grid[0].size() - 1];
16     }
17 };


Uriel 2014-01-19 02:57 鍙戣〃璇勮
]]>
[LeetCode]Plus One-2014.01.19http://www.shnenglu.com/Uriel/articles/205470.htmlUrielUrielSat, 18 Jan 2014 18:36:00 GMThttp://www.shnenglu.com/Uriel/articles/205470.htmlhttp://www.shnenglu.com/Uriel/comments/205470.htmlhttp://www.shnenglu.com/Uriel/articles/205470.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205470.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205470.html
 1 class Solution {
 2 public:
 3     vector<int> plusOne(vector<int> &digits) {
 4         reverse(digits.begin(), digits.end());
 5         int n = digits.size();
 6         int tp = 1;
 7         for(int i = 0; i < n; ++i) {
 8             digits[i] += tp;
 9             if(digits[i] == 10) {
10                 digits[i] = 0;
11                 tp = 1;
12             }
13             else {
14                 tp = 0;
15                 break;
16             }
17         }
18         if(tp) digits.push_back(tp);
19         reverse(digits.begin(), digits.end());
20         return digits;
21     }
22 };


Uriel 2014-01-19 02:36 鍙戣〃璇勮
]]>
[LeetCode]Combinations-2014.01.19http://www.shnenglu.com/Uriel/articles/205469.htmlUrielUrielSat, 18 Jan 2014 17:36:00 GMThttp://www.shnenglu.com/Uriel/articles/205469.htmlhttp://www.shnenglu.com/Uriel/comments/205469.htmlhttp://www.shnenglu.com/Uriel/articles/205469.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205469.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205469.html
 1 class Solution {
 2 public:
 3     vector<vector<int>> res;
 4     int nt;
 5     
 6     void DFS(int n, int k, vector<int> &tp, int pos) {
 7         if(nt == k) {
 8             res.push_back(tp);
 9             return;
10         }
11         for(int i = pos; i <= n; ++i) {
12             tp.push_back(i);
13             nt++;
14             DFS(n, k, tp, i + 1);
15             tp.pop_back();
16             nt--;
17         }
18     }
19         
20     vector<vector<int> > combine(int n, int k) {
21         vector<int> tp;
23         res.clear();
24         nt = 0;
25         DFS(n, k, tp, 1);
26         return res;
27     }
28 };


Uriel 2014-01-19 01:36 鍙戣〃璇勮
]]>
[LeetCode]Subsets [& ||]-2014.01.18http://www.shnenglu.com/Uriel/articles/205467.htmlUrielUrielSat, 18 Jan 2014 16:59:00 GMThttp://www.shnenglu.com/Uriel/articles/205467.htmlhttp://www.shnenglu.com/Uriel/comments/205467.htmlhttp://www.shnenglu.com/Uriel/articles/205467.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205467.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205467.htmlSubsets姹傞泦鍚圫鐨勬墍鏈夊瓙闆嗭紝綆楁槸浣嶈繍綆楀簲鐢ㄧ殑棰橈紵DFS涔熻

 1 class Solution {
 2 public:
 3     vector<vector<int> > subsets(vector<int> &S) {
 4         vector<vector<int> > res;
 5         sort(S.begin(), S.end());
 6         for(int i = 0; i < (1 << S.size()); ++i) {
 7             vector<int> tp;
 8             for(int j = 0; j < S.size(); ++j) {
 9                 if((1 << j) & i) tp.push_back(S[j]);
10             }
11             res.push_back(tp);
12         }
13         return res;
14     }
15 };

Subsets II
涔熸槸姹傞泦鍚圫鐨勬墍鏈夊瓙闆嗭紝浣嗘槸S涓彲鑳戒細鏈夌浉鍚屽厓绱狅紝鎵浠ユ眰鐨勬槸涓嶉噸澶嶇殑瀛愰泦
浣嶈繍綆楁灇涓劇劧鍚庡啀鍒や笉鐭ラ亾琛屼笉琛岋紝鍋鋒噿鐢―FS浜嗭紝鐒跺悗鍒ら噸浠涔堢殑榪樼籂緇撳崐澶╂病鎼炲畾錛屽弬鑰冧簡涓嬪埆浜虹殑鍐欐硶

 1 class Solution {
 2 public:
 3     vector<vector<int>> res;
 4 
 5     void DFS(vector<int> &S,vector<int> &tp,int pos) {
 6         res.push_back(tp);
 7         for(int i = pos; i < S.size(); i++) {
 8             while(i != pos && i < S.size() && S[i] == S[i-1]) i++;
 9             if(i == S.size()) {
10                 return;
11             }
12             tp.push_back(S[i]);
13             DFS(S, tp, i + 1);
14             tp.pop_back();
15         }
16     }
17     
18     vector<vector<int> > subsetsWithDup(vector<int> &S) {
19         vector<int> tp;
20         sort(S.begin(), S.end());
21         res.clear();
22         DFS(S, tp, 0);
23         return res;
24     }
25 };


Uriel 2014-01-19 00:59 鍙戣〃璇勮
]]>
[LeetCode]Path Sum [& ||]-2014.01.18http://www.shnenglu.com/Uriel/articles/205462.htmlUrielUrielSat, 18 Jan 2014 10:06:00 GMThttp://www.shnenglu.com/Uriel/articles/205462.htmlhttp://www.shnenglu.com/Uriel/comments/205462.htmlhttp://www.shnenglu.com/Uriel/articles/205462.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205462.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205462.html緇欎竴媯典簩鍙夋爲鍜屼竴涓暣鏁幫紝浜屽弶鏍戜腑姣忎釜鑺傜偣鏈変釜鏉冨鹼紝姣忔潯浠庢牴鑺傜偣鍒板彾鑺傜偣鐨勮礬鏉冨兼眰鍜岋紝闂槸鍚﹀瓨鍦ㄦ潈鍊煎拰絳変簬緇欏畾鏁扮殑
BFS錛岃褰曟潈鍊煎拰錛屽埌鍙惰妭鐐圭殑鏃跺欏垽鏂槸鍚︾瓑浜庣粰瀹氱殑鏁?br />
 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     struct Que {
13         int s;
14         TreeNode *p;
15     }q[100010];
16             
17     bool hasPathSum(TreeNode *root, int sum) {
18         int l = 0, r = 1, res = 0;
19         if(root == NULL) return 0;
20         q[0].p = root;
21         q[0].s = root->val;
22         while(l < r) {
23             if(q[l].p->left == NULL && q[l].p->right == NULL) {
24                 if(q[l].s == sum) return true;
25             }
26             if(q[l].p->left != NULL) {
27                 q[r].p = q[l].p->left;
28                 q[r].s = q[l].s + q[r].p->val;
29                 ++r;
30             }
31             if(q[l].p->right != NULL) {
32                 q[r].p = q[l].p->right;
33                 q[r].s = q[l].s + q[r].p->val;
34                 ++r;
35             }
36             ++l;
37         }
38         return false;
39     }
40 };

Path Sum II
涓婁竴棰樼殑鍔犲己鐗堬紝瑕佽緭鍑烘墍鏈夋潈鍊煎拰絳変簬緇欏畾鏁存暟鐨勮礬寰勶紝寮涓彉閲忚褰曢槦鍒楀厓绱犱箣鍓嶄竴涓亶鍘嗙殑鑺傜偣錛屽埌鍙跺瓙鑺傜偣鏃惰嫢鏉冨煎拰絳変簬緇欏畾鏁幫紝鍒欓掑綊鎵懼嚭璺緞

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     struct Que {
13         int s, fa;
14         TreeNode *p;
15     }q[100010];
16             
17     vector<vector<int> > pathSum(TreeNode *root, int sum) {
18         int l = 0, r = 1;
19         vector<vector<int> > res;
20         res.clear();
21         if(root == NULL) return res;
22         q[0].p = root;
23         q[0].fa = -1;
24         q[0].s = root->val;
25         while(l < r) {
26             if(q[l].p->left == NULL && q[l].p->right == NULL) {
27                 if(q[l].s == sum) {
28                     vector<int> t;
29                     int tp = l;
30                     while(tp >= 0) {
31                         t.push_back(q[tp].p->val);
32                         tp = q[tp].fa;
33                     }
34                     reverse(t.begin(), t.end());
35                     res.push_back(t);
36                 }
37             }
38             if(q[l].p->left != NULL) {
39                 q[r].p = q[l].p->left;
40                 q[r].fa = l;
41                 q[r].s = q[l].s + q[r].p->val;
42                 ++r;
43             }
44             if(q[l].p->right != NULL) {
45                 q[r].p = q[l].p->right;
46                 q[r].fa = l;
47                 q[r].s = q[l].s + q[r].p->val;
48                 ++r;
49             }
50             ++l;
51         }
52         return res;
53     }
54 };


Uriel 2014-01-18 18:06 鍙戣〃璇勮
]]>
[LeetCode]Sum Root to Leaf Numbers-2014.01.18http://www.shnenglu.com/Uriel/articles/205461.htmlUrielUrielSat, 18 Jan 2014 09:49:00 GMThttp://www.shnenglu.com/Uriel/articles/205461.htmlhttp://www.shnenglu.com/Uriel/comments/205461.htmlhttp://www.shnenglu.com/Uriel/articles/205461.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205461.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205461.html渚嬪    1
        /  \
      2    3
sum=12+13=25
BFS錛屾瘡涓槦鍒楀厓绱犺褰曚粠鏍瑰埌璇ヨ妭鐐逛負姝㈢殑鏁存暟澶у皬錛屾瘡嬈?10+val錛岄亣鍒板彾鑺傜偣灝辯瘡鍔?br />
 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     struct Que {
13         int s;
14         TreeNode *p;
15     }q[100010];
16             
17     int sumNumbers(TreeNode *root) {
18         int l = 0, r = 1, res = 0;
19         if(root == NULL) return 0;
20         q[0].p = root;
21         q[0].s = root->val;
22         while(l < r) {
23             if(q[l].p->left == NULL && q[l].p->right == NULL) {
24                 res += q[l].s;
25             }
26             if(q[l].p->left != NULL) {
27                 q[r].p = q[l].p->left;
28                 q[r].s = q[l].s * 10 + q[r].p->val;
29                 ++r;
30             }
31             if(q[l].p->right != NULL) {
32                 q[r].p = q[l].p->right;
33                 q[r].s = q[l].s * 10 + q[r].p->val;
34                 ++r;
35             }
36             ++l;
37         }
38         return res;
39     }
40 };


Uriel 2014-01-18 17:49 鍙戣〃璇勮
]]>
[LeetCode]Validate Binary Search Tree-2014.01.18http://www.shnenglu.com/Uriel/articles/205460.htmlUrielUrielSat, 18 Jan 2014 09:36:00 GMThttp://www.shnenglu.com/Uriel/articles/205460.htmlhttp://www.shnenglu.com/Uriel/comments/205460.htmlhttp://www.shnenglu.com/Uriel/articles/205460.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205460.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205460.html
 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool res;
13     bool DFS(TreeNode *root, int l, int r) {
14         if(root == NULL) return true;
15         else
16             return root->val > l && root->val < r && DFS(root->left, l, root->val) && DFS(root->right, root->val, r);
17     }
18     bool isValidBST(TreeNode *root) {
19         return DFS(root, -1000000, 1000000);
20     }
21 };


Uriel 2014-01-18 17:36 鍙戣〃璇勮
]]>
[LeetCode]Minimum Depth of Binary Tree-2014.01.18http://www.shnenglu.com/Uriel/articles/205459.htmlUrielUrielSat, 18 Jan 2014 08:56:00 GMThttp://www.shnenglu.com/Uriel/articles/205459.htmlhttp://www.shnenglu.com/Uriel/comments/205459.htmlhttp://www.shnenglu.com/Uriel/articles/205459.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205459.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205459.html
 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     struct Que {
13         int depth;
14         TreeNode *p;
15     }q[100010];
16         
17     int minDepth(TreeNode *root) {
18         int l = 0, r = 1, mx = 0, mi = 1000000;
19         if(root == NULL) return 0;
20         q[0].p = root;
21         q[0].depth = 1;
22         while(l < r) {
23             if(q[l].p->left == NULL && q[l].p->right == NULL) {
24                 if(q[l].depth < mi) mi = q[l].depth;
25             }
26             if(q[l].p->left != NULL) {
27                 q[r].p = q[l].p->left;
28                 q[r].depth = q[l].depth + 1;
29                 ++r;
30             }
31             if(q[l].p->right != NULL) {
32                 q[r].p = q[l].p->right;
33                 q[r].depth = q[l].depth + 1;
34                 ++r;
35             }
36             ++l;
37         }
38         return mi;
39     }
40 };


Uriel 2014-01-18 16:56 鍙戣〃璇勮
]]>
[LeetCode]Balanced Binary Tree-2014.01.18http://www.shnenglu.com/Uriel/articles/205458.htmlUrielUrielSat, 18 Jan 2014 08:55:00 GMThttp://www.shnenglu.com/Uriel/articles/205458.htmlhttp://www.shnenglu.com/Uriel/comments/205458.htmlhttp://www.shnenglu.com/Uriel/articles/205458.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205458.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205458.html浜庢槸DFS錛屽垎鍒壘涓ら瀛愭爲涓妭鐐圭殑鏈澶ф繁搴︼紝鍒ょ浉宸槸鍚︿笉澶т簬1鍗沖彲

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool res;
13     int DFS(TreeNode *root) {
14         if(!res || root == NULL) return 0;
15         int ld = DFS(root->left);
16         int rd = DFS(root->right);
17         if(abs(ld - rd) > 1) res = false;
18         return max(ld, rd) + 1;
19     }
20     
21     bool isBalanced(TreeNode *root) {
22         if(root == NULL) return true;
23         res = true;
24         DFS(root);
25         return res;
26     }
27 };


Uriel 2014-01-18 16:55 鍙戣〃璇勮
]]>
[LeetCode]Binary Tree Inorder Traversal-2014.01.18http://www.shnenglu.com/Uriel/articles/205453.htmlUrielUrielFri, 17 Jan 2014 18:44:00 GMThttp://www.shnenglu.com/Uriel/articles/205453.htmlhttp://www.shnenglu.com/Uriel/comments/205453.htmlhttp://www.shnenglu.com/Uriel/articles/205453.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205453.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205453.html
 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<int> res;
13 
14     void inorder(TreeNode *root) {
15         if(root->left != NULL) inorder(root->left);
16         res.push_back(root->val);
17         if(root->right != NULL) inorder(root->right);
18     }
19     
20     vector<int> inorderTraversal(TreeNode *root) {
21         res.clear();
22         if(root == NULL) return res;
23         inorder(root);
24         return res;
25     }
26 };


Uriel 2014-01-18 02:44 鍙戣〃璇勮
]]>
[LeetCode]Surrounded Regions-2014.01.17http://www.shnenglu.com/Uriel/articles/205447.htmlUrielUrielFri, 17 Jan 2014 10:16:00 GMThttp://www.shnenglu.com/Uriel/articles/205447.htmlhttp://www.shnenglu.com/Uriel/comments/205447.htmlhttp://www.shnenglu.com/Uriel/articles/205447.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205447.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205447.html棰樼洰寰堢畝鍗曪紝灝辨槸涓涓瓧絎﹂樀錛岀敱X鍜孫鏋勬垚錛屾壘鍑烘墍鏈塐鏋勬垚鐨勮繛閫氬尯鍩燂紝鑻ヨ鍖哄煙鎵鏈塐閮藉湪瀛楃闃靛唴閮?涓嶅湪杈圭晫涓?錛屽垯鎶婅榪為氬尯鍩熸墍鏈塐閮芥崲鎴怷錛屽叾浣欎笉鍙?br />
榪欓涓鐪嬪埌灝辨兂鐫鎬庝箞榪欎箞瑁哥殑DFS錛屼簬鏄兂涔熸病鎯蟲暡瀹岀洿鎺E錛岀湅RE榪斿洖鐨勬槸絀烘暟鎹紝浜庢槸鍔犱簡鍒oard涓虹┖鐨勮鍙ワ紝浜や笂鍘昏繕鏄疪E錛屽洜涓轟箣鍓嶅仛LeetCode榪欑RE鐨勬儏鍐甸兘鏄病鏈夊垽絀猴紝浜庢槸鎹簡N縐嶅啓娉曞垽絀猴紝榪樻槸RE

鍥犱負鏄紑浜嗕釜鏁扮粍璁板綍瀛楃闃墊瘡涓厓绱犳槸鍚﹁闂繃錛屽張寮浜嗗彟涓涓暟緇勫垽璇ヨ繛閫氬尯鍩熸槸鍚﹂偦杈癸紝鎯崇潃鏄笉鏄暟緇勫紑澶ぇ浜嗭紝浜庢槸璇曚簡鍚勭鏁扮粍澶у皬錛屽彂鐜板紑bool鍨嬶紝鍙紑涓涓暟緇勮兘榪囷紝鍚庢潵鎯沖埌鍏跺疄涓嶉渶瑕佸彟寮涓涓暟緇勮褰曟槸鍚﹂偦杈癸紝鎼滅殑鏃跺欏彧鎼滃洓杈逛笂涓篛鐨勯偅浜涗綅緗紝榪欐牱涓瀹氭槸閭昏竟鐨勶紝榪欎釜鏃墮棿澶嶆潅搴︿篃灝忎竴浜涳紝浜庢槸鏀逛簡錛岃繕鏄疪E錛岃屼笖鎸傚湪澶ф暟鎹笂錛屼絾鏄垜騫舵病鏈夊紑鏁扮粍錛屼笉浼氭湁瓚婄晫鍙戠敓錛岀劧鍚庡氨鍚勭鍚愯鐨勬敼鏉ユ敼鍘伙紝緇堜簬AC錛岃櫧鐒惰繕鏄緢鑾悕...涓哄暐涔嬪墠浼氭姤RE鍛紵

榪欐槸AC鐨勪唬鐮侊細

 1 class Solution {
 2 public:
 3     void DFS(int x, int y, vector<vector<char>> &board) {
 4         int tx, ty;
 5         if(x >= board.size() || y >= board[0].size() || x < 0 || y < 0 || board[x][y] != 'O') return;
 6         board[x][y] = 'Q';
 7         DFS(x, y + 1, board);
 8         DFS(x, y - 1, board);
 9         DFS(x + 1, y, board);
10         DFS(x - 1, y, board);
11     }
12     
13     void solve(vector<vector<char>> &board) {
14         if(board.empty()) return;
15         int row = board.size();
16         if(!row) return;
17         int col = board[0].size();
18         if(!col) return;
19         for(int i = 0; i < row; ++i) {
20             if(board[i][0] == 'O') DFS(i, 0, board);
21             if(board[i][col - 1] == 'O') DFS(i, col - 1, board);
22         }
23         for(int i = 0; i < col; ++i) {
24             if(board[0][i] == 'O') DFS(0, i, board);
25             if(board[row - 1][i] == 'O') DFS(row - 1, i, board);
26         }
27         for(int i = 0 ; i < row; ++i) {
28             for(int j = 0; j < col; ++j) {
29                 if(board[i][j] == 'Q') board[i][j] = 'O';
30                 else if(board[i][j] == 'O') board[i][j] = 'X';
31             }
32         }
33     }
34 
35 };


Uriel 2014-01-17 18:16 鍙戣〃璇勮
]]>
[LeetCode]Candy-2014.01.17http://www.shnenglu.com/Uriel/articles/205434.htmlUrielUrielThu, 16 Jan 2014 18:49:00 GMThttp://www.shnenglu.com/Uriel/articles/205434.htmlhttp://www.shnenglu.com/Uriel/comments/205434.htmlhttp://www.shnenglu.com/Uriel/articles/205434.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205434.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205434.html浠庡ご鍒板熬錛屼粠灝懼埌澶存壂涓ら亶鍗沖彲

 1 class Solution {
 2 public:
 3     int candy(vector<int> &ratings) {
 4         int tp = 1, res = ratings.size(), mi = 1;
 5         int can[100010];
 6         memset(can, 0, sizeof(can));
 7         if(ratings.empty()) return 0;
 8         for(int i = 1; i < ratings.size(); ++i) {
 9             if(ratings[i] > ratings[i - 1]) {
10                 can[i] = tp++;
11             }
12             else
13                 tp = 1;
14         }
15         tp = 1;
16         for(int i = ratings.size() - 2; i >= 0; --i) {
17             if(ratings[i] > ratings[i + 1]) {
18                 can[i] = max(tp++, can[i]);
19             }
20             else
21                 tp = 1;
22         }
23         for(int i = 0; i < ratings.size(); ++i) res += can[i];
24         return res;
25     }
26 };


Uriel 2014-01-17 02:49 鍙戣〃璇勮
]]>
[LeetCode]Distinct Subsequences-2014.01.16http://www.shnenglu.com/Uriel/articles/205431.htmlUrielUrielThu, 16 Jan 2014 11:19:00 GMThttp://www.shnenglu.com/Uriel/articles/205431.htmlhttp://www.shnenglu.com/Uriel/comments/205431.htmlhttp://www.shnenglu.com/Uriel/articles/205431.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205431.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205431.html鏄庢樉鐨凩CS鎵╁睍錛岃櫧鐒惰繕鏄悶浜嗗緢涔咃紝RE涓嬈℃暟緇勫紑澶皬錛屼互鍚庣湅榪欑鐩存帴婊氬姩鏁扮粍銆傘?br />
 1 int dp[2][122010];
 2 int numDistinct(string S, string T) {
 3     int res = 0;
 4     memset(dp, 0, sizeof(dp));
 5     dp[0][0] = dp[1][0] = 1;
 6     for(int i = 1; i <= S.length(); ++i) {
 7         for(int j = 1; j <= T.length(); ++j) {
 8             if(S[i - 1] == T[j - 1]) {
 9                 dp[i & 1][j] = dp[(i - 1) & 1][j - 1] + dp[(i - 1) & 1][j];
10             }
11             else
12                 dp[i & 1][j] = dp[(i - 1) & 1][j];
13         }
14     }
15     return dp[S.length() & 1][T.length()];
16 }


Uriel 2014-01-16 19:19 鍙戣〃璇勮
]]>
[LeetCode]Single Number II-2014.01.16http://www.shnenglu.com/Uriel/articles/205406.htmlUrielUrielWed, 15 Jan 2014 19:25:00 GMThttp://www.shnenglu.com/Uriel/articles/205406.htmlhttp://www.shnenglu.com/Uriel/comments/205406.htmlhttp://www.shnenglu.com/Uriel/articles/205406.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205406.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205406.html
 1 class Solution {
 2 public:
 3     int singleNumber(int A[], int n) {
 4         unsigned int res = 0;
 5         for(int j = 0; j < 32; ++j) {
 6             int tp = 0;
 7             const unsigned int pos = 1 << j;
 8             for(int i = 0; i < n; ++i) {
 9                 tp += (A[i] & pos) > 0;
10             }
11             res |= (!(tp % 3)) ? 0 : pos;
12         }
13         return (int)res;
14     }
15 };

鍙︿竴縐嶆瘮杈冮珮澶т笂鐨勫仛娉曟槸寮涓変釜鍙橀噺錛屽垎鍒爣璁板嚭鐜拌繃涓嬈★紝涓ゆ錛屼笁嬈$殑錛屾瘡澶勭悊涓涓暟鐨勬椂鍊欏垎鍒綆楀紓鎴栥佷笌銆侀潪[鍑虹幇涓夋鏃跺墠涓や釜鍙橀噺閮戒負1錛屽彇鍙嶅悗鍒╃敤絎笁涓彉閲忔竻闄よ鏁癩錛岀劧鍚庣涓涓彉閲忎腑鍓╀笅鐨勫氨鏄彧鍑虹幇榪囦竴嬈$殑鏁?br />浠g爜宸ㄤ紭緹巭

 1 class Solution {
 2 public:
 3     int singleNumber(int A[], int n) {
 4         int tp3, tp1 = 0, tp2 = 0;
 5         for(int i = 0; i < n; ++i) {
 6             tp2 |= tp1 & A[i];
 7             tp1 ^= A[i];
 8             tp3 = ~(tp1 & tp2);
 9             tp1 &= tp3;
10             tp2 &= tp3;
11         }
12         return tp1;
13     }
14 };


Uriel 2014-01-16 03:25 鍙戣〃璇勮
]]>
[LeetCode]Single Number-2014.01.16http://www.shnenglu.com/Uriel/articles/205405.htmlUrielUrielWed, 15 Jan 2014 19:19:00 GMThttp://www.shnenglu.com/Uriel/articles/205405.htmlhttp://www.shnenglu.com/Uriel/comments/205405.htmlhttp://www.shnenglu.com/Uriel/articles/205405.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205405.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205405.htmlhttp://www.cnblogs.com/feiling/p/3349654.html

灝辨槸涓嶆柇鍋氬紓鎴栬繍綆楀氨濂斤紝鏈鍚庡墿涓嬬殑灝辨槸鍙嚭鐜頒簡1嬈$殑閭d釜鏁?br />
 1 class Solution {
 2 public:
 3     int singleNumber(int A[], int n) {
 4         int tp = 0;
 5         if(!n) return 0;
 6         for(int i = 0; i < n; ++i) {
 7             tp = tp ^ A[i];
 8         }
 9         return tp;
10     }
11 };


Uriel 2014-01-16 03:19 鍙戣〃璇勮
]]>
[LeetCode]Word Break II-2014.01.13http://www.shnenglu.com/Uriel/articles/205353.htmlUrielUrielMon, 13 Jan 2014 19:13:00 GMThttp://www.shnenglu.com/Uriel/articles/205353.htmlhttp://www.shnenglu.com/Uriel/comments/205353.htmlhttp://www.shnenglu.com/Uriel/articles/205353.html#Feedback1http://www.shnenglu.com/Uriel/comments/commentRss/205353.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205353.html
 1 class Solution {
 2 public:
 3     int max_len, min_len;
 4     vector<string> res;
 5     vector<string> tp_res;
 6     
 7     void save_sentence() {
 8         string s = "";
 9         for(int i = tp_res.size() - 1; i >= 0; --i) s += tp_res[i] + string(" ");
10         s.pop_back();
11         res.push_back(s);
12     }
13     
14     void sov(string s, unordered_set<string> &dic) {
15         int len = s.size();
16         if(!len) {
17             save_sentence();
18             return;
19         }
20         for(int i = min_len; i <= min(max_len, len); ++i) {
21             string tp = s.substr(len - i);
22             if(dic.find(tp) != dic.end()) {
23                 tp_res.push_back(tp);
24                 sov(s.substr(0, len - i), dic);
25                 tp_res.pop_back();
26             }
27         }
28     }
29     
30     vector<string> wordBreak(string s, unordered_set<string> &dic) {
31         res.clear();
32         min_len = 100000, max_len = 0;
33         unordered_set<string>::iterator it;
34         for(it = dic.begin(); it != dic.end(); ++it) {
35             max_len = max_len > it->size() ? max_len : it->size();
36             min_len = min_len < it->size() ? min_len : it->size();
37         }
38         sov(s, dic);
39         return res;
40     }
41 };


Uriel 2014-01-14 03:13 鍙戣〃璇勮
]]>
[LeetCode]Word Break-2014.01.13http://www.shnenglu.com/Uriel/articles/205352.htmlUrielUrielMon, 13 Jan 2014 19:10:00 GMThttp://www.shnenglu.com/Uriel/articles/205352.htmlhttp://www.shnenglu.com/Uriel/comments/205352.htmlhttp://www.shnenglu.com/Uriel/articles/205352.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205352.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205352.html
 1 class Solution {
 2 public:
 3     int dp[100010];
 4     bool wordBreak(string s, unordered_set<string> &dict) {
 5         int i, j;
 6         memset(dp, -1, sizeof(dp));
 7         dp[0] = 0;
 8         unordered_set<string>::iterator it;
 9         for(i = 0; i <= s.length(); ++i) {
10             for(j = 0, it = dict.begin(); it != dict.end(); ++it, ++j) {
11                 int ll = it->size();
12                 if(dp[i] >= 0 && i + ll <= s.length() && s.substr(i, ll) == *it) {
13                     dp[i + ll] = j;
14                 }
15             }
16         }
17         if(~dp[s.length()]) return true;
18         return false;
19     }
20 };


Uriel 2014-01-14 03:10 鍙戣〃璇勮
]]>
[LeetCode]Unique Binary Search Trees II-2014.01.12http://www.shnenglu.com/Uriel/articles/205328.htmlUrielUrielSun, 12 Jan 2014 07:30:00 GMThttp://www.shnenglu.com/Uriel/articles/205328.htmlhttp://www.shnenglu.com/Uriel/comments/205328.htmlhttp://www.shnenglu.com/Uriel/articles/205328.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205328.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205328.html瀵箆ector榪樺緢涓嶇啛緇冨晩...鍐欐檿浜嗕箣鍚庤繕鏄弬鑰冧簡涓涓嬪埆浜虹殑浠g爜

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<TreeNode *> sov(int l, int r) {
13         vector<TreeNode *> ans;
14         if(l > r) ans.push_back(NULL);
15         else {
16             for(int i = l; i <= r; ++i) {
17                 vector<TreeNode *> left = sov(l, i - 1);
18                 vector<TreeNode *> right = sov(i + 1, r);
19                 for(int ll = 0; ll < left.size(); ++ll) {
20                     for(int rr = 0; rr < right.size(); ++rr) {
21                         TreeNode *root = new TreeNode(i);
22                         root->left = left[ll];
23                         root->right = right[rr];
24                         ans.push_back(root);
25                     }
26                 }
27             }
28         }
29         return ans;
30     }
31     
32     vector<TreeNode *> generateTrees(int n) {
33         return sov(1, n);
34     }
35 };


Uriel 2014-01-12 15:30 鍙戣〃璇勮
]]>
[LeetCode]Unique Binary Search Trees-2014.01.12http://www.shnenglu.com/Uriel/articles/205318.htmlUrielUrielSat, 11 Jan 2014 17:16:00 GMThttp://www.shnenglu.com/Uriel/articles/205318.htmlhttp://www.shnenglu.com/Uriel/comments/205318.htmlhttp://www.shnenglu.com/Uriel/articles/205318.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205318.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205318.html姘撮鍚х畻鏄紝瀵逛簬n涓妭鐐圭殑BST錛?~n閮芥湁鍙兘鏄叾鏍戞牴錛屼簬鏄?~n杞祦鍋氭牴錛屽亣璁炬鏃舵爲鏍逛負i錛屽垯浠涓烘牴鐨刵涓妭鐐圭殑BST鏁扮洰涓篿-1涓妭鐐圭殑BST鏁扮洰涔樹互n-i涓妭鐐圭殑BST鏁扮洰

 1 class Solution {
 2 public:
 3     int dp[100010];
 4     int numTrees(int n) {
 5         dp[0] = dp[1] = 1;
 6         for(int i = 2; i <= n; ++i) {
 7             dp[i] = 0;
 8             for(int j = 1; j <= i; ++j) {
 9                 dp[i] += dp[j - 1] * dp[i - j];
10             }
11         }
12         return dp[n];
13     }
14 };


Uriel 2014-01-12 01:16 鍙戣〃璇勮
]]>
[LeetCode]Gas Station-2014.01.09http://www.shnenglu.com/Uriel/articles/205305.htmlUrielUrielFri, 10 Jan 2014 19:01:00 GMThttp://www.shnenglu.com/Uriel/articles/205305.htmlhttp://www.shnenglu.com/Uriel/comments/205305.htmlhttp://www.shnenglu.com/Uriel/articles/205305.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205305.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205305.htmlhttp://blog.csdn.net/lbyxiafei/article/details/12183461

 1 class Solution {
 2 public:
 3     int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
 4         int sum = 0, cnt = 0, st = 0;
 5         for(int i = 0; i < gas.size(); ++i) {
 6             sum += gas[i] - cost[i];
 7             cnt += gas[i] - cost[i];
 8             if(sum < 0) {
 9                 sum = 0;
10                 st = (i + 1) % gas.size();
11             }
12         }
13         if(cnt < 0) return -1;
14         return st;
15     }
16 };


Uriel 2014-01-11 03:01 鍙戣〃璇勮
]]>
[LeetCode]Longest Consecutive Sequence-2014.01.09http://www.shnenglu.com/Uriel/articles/205304.htmlUrielUrielFri, 10 Jan 2014 18:57:00 GMThttp://www.shnenglu.com/Uriel/articles/205304.htmlhttp://www.shnenglu.com/Uriel/comments/205304.htmlhttp://www.shnenglu.com/Uriel/articles/205304.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205304.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205304.htmlmap鐨勫簲鐢紵閮藉繕璁頒簡map浜?..鐪嬩簡瑙i鎶ュ憡綆楀紡澶嶄範涓涓嬶紵

 1 class Solution {
 2 public:
 3     int longestConsecutive(vector<int> &num) {
 4         map<intint> mp;
 5         mp.clear();
 6         int n = num.size();
 7         for(int i = 0; i < n; ++i) {
 8             mp.insert(pair<intint>(num[i], 1));
 9         }
10         int mx = 1, pre_k = 0, pre_v = 0;
11         map<intint>::iterator it;
12         for(it = mp.begin(); it != mp.end(); ++it) {
13             if(it == mp.begin()) {
14                 pre_k = it->first;
15                 pre_v = it->second;
16                 continue;
17             }
18             if(it->first == pre_k + 1) {
19                 it->second = pre_v + 1;
20                 mx = max(mx, it->second);
21             }
22             pre_k = it->first;
23             pre_v = it->second;
24         }
25         return mx;
26     }
27 };


Uriel 2014-01-11 02:57 鍙戣〃璇勮
]]>
[LeetCode]Binary Tree Preorder Traversal-2014.01.06http://www.shnenglu.com/Uriel/articles/205303.htmlUrielUrielFri, 10 Jan 2014 18:54:00 GMThttp://www.shnenglu.com/Uriel/articles/205303.htmlhttp://www.shnenglu.com/Uriel/comments/205303.htmlhttp://www.shnenglu.com/Uriel/articles/205303.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205303.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205303.html娉ㄦ剰鍒ょ┖鏍戯紒錛侊紒

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10  
11 vector<int> que;
12 
13 void pre_order(TreeNode *root) {
14     if(root == NULL) return;
15     que.push_back(root->val);
16     if(root->left != NULL) pre_order(root->left);
17     if(root->right != NULL) pre_order(root->right);
18 }
19     
20 class Solution {
21 public:
22     vector<int> preorderTraversal(TreeNode *root) {
23         if(!que.empty()) que.clear();
24         pre_order(root);
25         return que;
26     }
27 };


Uriel 2014-01-11 02:54 鍙戣〃璇勮
]]>
[LeetCode]Binary Tree Postorder Traversal-2014.01.06http://www.shnenglu.com/Uriel/articles/205302.htmlUrielUrielFri, 10 Jan 2014 18:52:00 GMThttp://www.shnenglu.com/Uriel/articles/205302.htmlhttp://www.shnenglu.com/Uriel/comments/205302.htmlhttp://www.shnenglu.com/Uriel/articles/205302.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205302.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205302.htmlRE鏃犵┓澶氭錛屾敞鎰忓垽絀烘爲錛侊紒錛?br />
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 
*/
class Solution {
public:
    TreeNode *tp[1010];
    vector<int> postorderTraversal(TreeNode *root) {
        int k = 0;
        vector<int> que;
        if(root == NULL) return que;
        TreeNode *p = root, *pre = NULL;
        tp[k++] = p;
        while(k) {
            TreeNode *cur = tp[k - 1];
            if((cur->left == NULL && cur->right == NULL) || (pre != NULL && (pre == cur->left || pre == cur->right))) {
                que.push_back(cur->val);
                --k;
                pre = cur;
            }
            else {
                if(cur->right != NULL) tp[k++] = cur->right;
                if(cur->left != NULL) tp[k++] = cur->left;
            }
        }
        return que;
    }
};


Uriel 2014-01-11 02:52 鍙戣〃璇勮
]]>
[LeetCode]Evaluate Reverse Polish Notation-2013.12.27http://www.shnenglu.com/Uriel/articles/205301.htmlUrielUrielFri, 10 Jan 2014 18:51:00 GMThttp://www.shnenglu.com/Uriel/articles/205301.htmlhttp://www.shnenglu.com/Uriel/comments/205301.htmlhttp://www.shnenglu.com/Uriel/articles/205301.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/205301.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/205301.html緇欏嚭閫嗘嘗鍏板紡錛屾眰綆楀紡鐨勫鹼紝妯℃嫙鏍堬紝涔橀櫎鐨勬椂鍊欐敞鎰忓嚭鏍堥『搴忥紒鍜岀畻寮忕殑鎿嶄綔鏁伴『搴忕浉鍙嶏紒涓嬪崍灝辨鍦ㄨ繖涓婇潰浜?..

 1 class Solution {
 2 public:
 3     int evalRPN(vector<string> &tokens) {
 4         int stk[1010], k = 0;
 5         for(int i = 0; i < tokens.size(); ++i) {
 6             if(tokens[i] == "+") {
 7                 int a = stk[k - 1], b = stk[k - 2];
 8                 k -= 2;
 9                 stk[k++] = a + b;
10             }
11             else if(tokens[i] == "-") {
12                 int a = stk[k - 1], b = stk[k - 2];
13                 k -= 2;
14                 stk[k++] = b - a;
15             }
16             else if(tokens[i] == "*") {
17                 int a = stk[k - 1], b = stk[k - 2];
18                 k -= 2;
19                 stk[k++] = b * a;
20             }
21             else if(tokens[i] == "/") {
22                 int a = stk[k - 1], b = stk[k - 2];
23                 k -= 2;
24                 stk[k++] = b / a;
25             }
26             else {
27                 stk[k++] = atoi(tokens[i].c_str());
28             }
29         }
30         return stk[0];
31     }
32 };


Uriel 2014-01-11 02:51 鍙戣〃璇勮
]]>
97久久精品无码一区二区| 久久国产精品成人影院| 天天爽天天爽天天片a久久网| 少妇无套内谢久久久久| 国产999精品久久久久久| 成人免费网站久久久| 精品少妇人妻av无码久久| 久久久精品2019免费观看| 亚洲av成人无码久久精品| 亚洲AV乱码久久精品蜜桃| 亚洲精品无码久久千人斩| 午夜久久久久久禁播电影| 久久综合综合久久综合| 久久久久高潮毛片免费全部播放| 亚洲精品蜜桃久久久久久| 久久婷婷五月综合色高清| 久久久久亚洲Av无码专| MM131亚洲国产美女久久| 999久久久无码国产精品| 久久久久亚洲AV片无码下载蜜桃| 久久99精品久久久久久hb无码 | 久久精品草草草| 18岁日韩内射颜射午夜久久成人| 中文字幕成人精品久久不卡| 久久高清一级毛片| 日日狠狠久久偷偷色综合96蜜桃| 久久午夜免费视频| 久久香蕉超碰97国产精品| 精品一区二区久久| 久久久久久噜噜精品免费直播| 久久久久久国产精品美女| 国内精品久久久久久久久电影网| 蜜臀久久99精品久久久久久小说| 韩国免费A级毛片久久| 久久99久久成人免费播放| 久久只这里是精品66| 国产精品99久久精品| 色婷婷狠狠久久综合五月| 久久久亚洲欧洲日产国码二区| 久久国产成人午夜aⅴ影院| 2020久久精品亚洲热综合一本|