代码改变世界

leetcode - Generate Parentheses

2013-04-19 09:54 by 张汉生, 157 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述,点击此处class Solution {public: void dfs(int n, int before, int left, string cur, vector<string> &rlt){ int right = before - left; if (left<n){ dfs(n, before+1, left+1, cur+"(", rlt); } if (right<left){ if (before == 2*n-1) rlt.push_back(cur+")"); else dfs(n,befo 阅读全文

leetcode - Flatten Binary Tree to Linked List

2013-04-12 16:23 by 张汉生, 159 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处/** * 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 * getLastNode(TreeNode *root){ if (root == NULL) return NULL; TreeN... 阅读全文

leetcode - First Missing Positive

2013-04-12 16:06 by 张汉生, 149 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处利用到原来的数组空间,不知道符不符合题意。 1 class Solution { 2 public: 3 int firstMissingPositive(int A[], int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int i, j; 7 for (i=0; i<n; i++){ 8 if (A[i]<=0 || A[i]>n) 9 A[i] = 0;10 }11... 阅读全文

leetcode - Edit Distance

2013-04-11 12:49 by 张汉生, 150 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处这道题错在delete之后return cur[len2], 在我的机器上内存还没有及时释放,结果仍然是对的,但是提交之后会有两个测试过不了,这种错误少犯,太折磨人了。 1 class Solution { 2 public: 3 int minDistance(string word1, string word2) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int len1 = word1.length(); 7 ... 阅读全文

leetcode - Divide Two Integers

2013-03-28 22:59 by 张汉生, 173 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处 1 #include <limits.h> 2 class Solution { 3 public: 4 unsigned int maxInt = (unsigned int) INT_MAX; 5 unsigned int getAbsoluteValue(int n){ 6 if (n>=0 || n==INT_MIN) 7 return (unsigned int) n; 8 return maxInt + maxInt + 1 - (unsigned int)n +1; 9 }10 unsigned int divide... 阅读全文

leetcode - Distinct Subsequences

2013-03-28 21:49 by 张汉生, 160 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处 1 class Solution { 2 public: 3 int numDistinct(string S, string T) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int tlen = T.length(); 7 int * tag = new int[tlen+1]; 8 int i, j; 9 for (i=0; i<=tlen; i++)10 tag[i] = 0;... 阅读全文

leetcode - Decode Ways

2013-03-28 12:12 by 张汉生, 214 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处 1 class Solution { 2 public: 3 int numDecodings(string s) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int len = s.length(); 7 if (len ==0 || (len>=1&&s.at(0)=='0')) 8 return 0; 9 else if (len==1)10 return 1;11 ... 阅读全文

leetcode - Count and Say

2013-03-28 10:52 by 张汉生, 112 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处 1 class Solution { 2 public: 3 string genNext(string cur){ 4 string rlt = ""; 5 char t[20]; 6 int length = cur.length(); 7 int i = 0; 8 char c = cur.at(0); 9 int len = 0;10 while(i<length){11 while (i<length && cur.at(i)==c){12 len++;13 i... 阅读全文

leetcode - Convert Sorted List to Binary Search Tree

2013-03-28 10:23 by 张汉生, 114 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 /**10 * Definition for binary tree11 * struct TreeNode {12 * int val;13 * TreeNode *left;14 * Tr... 阅读全文

leetcode - Convert Sorted Array to Binary Search Tree

2013-03-28 10:12 by 张汉生, 141 阅读, 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 typedef vector<int>::iterator vi;13 TreeNode *sor... 阅读全文