代码改变世界

leetcode - Letter Combinations of a Phone Number

2013-04-25 22:10 by 张汉生, 355 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处 1 class Solution { 2 public: 3 string tables[8]={"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; 4 void dfs(vector<string> & result, int n, int i, string & digits, string cur){ 5 string str = 阅读全文

leetcode - Length of Last Word

2013-04-23 23:16 by 张汉生, 120 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处此题一时兴起,居然从前面开始扫描,真是疯了。 1 class Solution { 2 public: 3 int lengthOfLastWord(const char *s) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if (s==NULL) 7 return 0; 8 int i = 0; 9 int len = 0;10 int lastLen = 0;11 while... 阅读全文

leetcode - Largest Rectangle in Histogram

2013-04-23 23:04 by 张汉生, 207 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处此题需要仔细想想才知道怎么做,而且几个判断的地方使用乘法会溢出 1 class Solution { 2 public: 3 int largestRectangleArea(vector<int> &height) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int n = height.size(); 7 if (n<=0) 8 return 0; 9 int * hgt = new int... 阅读全文

leetcode - Jump Game II

2013-04-21 11:33 by 张汉生, 158 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处这道题的优化需要好好想想,自己想了半天才想出来,真笨。 1 class Solution { 2 public: 3 int jump(int A[], int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if (n<=0) 7 return true; 8 int *flag = new int[n]; 9 int *index = new int[n];10 index[n-1... 阅读全文

leetcode - Jump Game

2013-04-21 10:43 by 张汉生, 119 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处 1 class Solution { 2 public: 3 bool canJump(int A[], int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if (n<=0) 7 return true; 8 bool *flag = new bool[n]; 9 flag[n-1] = true;10 int i, j;11 for (i=0; i<n-1; i++)... 阅读全文

leetcode - Interleaving String

2013-04-21 10:32 by 张汉生, 152 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处 1 class Solution { 2 public: 3 bool isInterleave(string s1, string s2, string s3) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int len1 = s1.length(); 7 int len2 = s2.length(); 8 int len3 = s3.length(); 9 if (len1+len2 ... 阅读全文

leetcode - Integer to Roman

2013-04-19 12:00 by 张汉生, 188 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处 1 class Solution { 2 public: 3 string intToRoman(int num) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 string thousands[4]={"","M","MM","MMM"}; 7 string hundreds[10] = {"","C","CC" 阅读全文

leetcode - Insert Interval

2013-04-19 11:04 by 张汉生, 158 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处 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 class Solution {11 public:12 vector<Interval> insert(vector<Interval> &inter... 阅读全文

leetcode - Implement strStr()

2013-04-19 10:29 by 张汉生, 149 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处实现的是m*n的算法,KMP算法是m+n的 1 class Solution { 2 public: 3 char *strStr(char *haystack, char *needle) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int hayLen = strlen(haystack); 7 int neeLen = strlen(needle); 8 for (int i=0; i+neeLen<... 阅读全文

leetcode - Gray Code

2013-04-19 10:16 by 张汉生, 152 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处 1 class Solution { 2 public: 3 vector<int> grayCode(int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 vector<int> rlt; 7 rlt.push_back(0); 8 if (n<=0) 9 return rlt;10 int i;11 for (i=0; i<n; i++){12 int ad... 阅读全文
上一页 1 ··· 8 9 10 11 12 13 14 15 16 下一页