代码改变世界

leetcode - Container With Most Water

2013-03-26 22:48 by 张汉生, 173 阅读, 0 推荐, 收藏, 编辑
摘要:题目链接:http://oj.leetcode.com/problems/container-with-most-water/第一个解法是用两个数组双向记录。方法的时间复杂度是O(n+max)。这里的max是height的最大值。当时没有想到用双向遍历的方法去做。【貌似第一遍leetcode中还从来没有用过双向遍历】 1 class Solution { 2 public: 3 int maxArea(vector &height) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int mai... 阅读全文

leetcode - Construct Binary Tree from Preorder and Inorder Traversal

2013-03-26 21:05 by 张汉生, 155 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处 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 TreeNode * buildTree(vector<int>::iterator inBegi... 阅读全文

leetcode - Construct Binary Tree from Inorder and Postorder Traversal

2013-03-26 20:54 by 张汉生, 272 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处此题如果直接采用原来的BuildTree递归会有Memory limit Exceed 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 TreeNo... 阅读全文

leetcode - Combinations

2013-03-26 19:53 by 张汉生, 154 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处 1 class Solution { 2 public: 3 void solve(vector<vector<int>> &rlt, vector<int> cur, int k, int n){ 4 int last = 0; 5 if (!cur.empty()) 6 last = *(cur.end()-1); 7 for (int i= last+1; n-i>=k-1; i++){ 8 vector<int> tvc = cur; 9 ... 阅读全文

leetcode - Combination Sum II

2013-03-26 19:44 by 张汉生, 183 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处 1 class Solution { 2 public: 3 int * cts; 4 void getResult(vector<vector<int>> & rlt, vector<int> current, vector<int>* tags, int target){ 5 if (target <= 0) 6 return; 7 int maxN = 1000000000; 8 if (!current.empty()) 9 maxN = *(curren... 阅读全文

leetcode - Combination Sum

2013-03-26 19:11 by 张汉生, 160 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处 1 class Solution { 2 public: 3 void getResult(vector<vector<int>> & rlt, vector<int> current, vector<int>* tags, int target){ 4 if (target <= 0) 5 return; 6 int maxN = 1000000000; 7 if (!current.empty()) 8 maxN = *(current.end()-1); 9 ... 阅读全文

windows下统计某个目录下的源代码的行数

2013-03-15 23:39 by 张汉生, 1406 阅读, 0 推荐, 收藏, 编辑
摘要:1 #include <stdio.h> 2 #include <windows.h> 3 4 using namespace std; 5 6 int countLines(char * src){ 7 int count = 0; 8 WIN32_FIND_DATA findFileData; 9 char * toFind = new char[200];10 strcpy(toFind,src);11 strcat(toFind,"\\*.*");12 HANDLE hFind = FindFirstFile(toFind,&find 阅读全文

leetcode - Climbing Stairs

2013-03-05 21:32 by 张汉生, 187 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处 1 class Solution { 2 public: 3 int climbStairs(int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if (n<=0) 7 return 0; 8 if (n==1) 9 return 1;10 int current, lastTwo = 1,lastOne=1... 阅读全文

leetcode - Binary Tree Zigzag Level Order Traversal

2013-03-05 21:24 by 张汉生, 176 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处 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<vector<int> > zigzagLevelOrder(TreeNode *roo... 阅读全文

leetcode - Binary Tree Maximum Path Sum

2013-03-05 21:04 by 张汉生, 189 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处 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 getValue(TreeNode * node, int & endValue){13 ... 阅读全文