上一页 1 ··· 8 9 10 11 12 13 14 15 16 ··· 31 下一页
  2013年1月9日
摘要: 一开始写了个KMP算法,大计算量居然超时,后来用最简单的蛮力算法,居然过了。。class Solution {public: char *strStr(char *haystack, char *needle) { // Start typing your C/C++ solution below // DO NOT write int main() function int hayLen = strlen(haystack); int neeLen = strlen(needle); for (in... 阅读全文
posted @ 2013-01-09 23:07 kkmm 阅读(356) 评论(0) 推荐(0) 编辑
  2012年12月31日
摘要: The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. For example, given n = 2, return [0,1,3,2]. Its gray 阅读全文
posted @ 2012-12-31 11:59 kkmm 阅读(4768) 评论(0) 推荐(0) 编辑
摘要: class Solution {public: //目的是将binary tree变成前序遍历的list,但是变换过程是后序遍历 void flatten(TreeNode *root, TreeNode *&first, TreeNode *&last){ if (!root) return ; TreeNode *leftFirst = NULL; TreeNode *leftLast = NULL; TreeNode *rightFirst = NULL; TreeNode *righ... 阅读全文
posted @ 2012-12-31 11:32 kkmm 阅读(590) 评论(0) 推荐(0) 编辑
摘要: class Solution {public: //p是当前的string( 例如"((()" );k是当前长度,例如4;n是总长度;lMore是左括号比右括号多多少 void generateParenthesis(string p, int k, int n, int lMore){ if (lMore < 0) return; if (k == n){ if (lMore == 0) result.push_back(p); //如果满足条件,push_back并退出 ... 阅读全文
posted @ 2012-12-31 11:32 kkmm 阅读(536) 评论(0) 推荐(0) 编辑
摘要: 先给一个例子,两个字符串eeba和abca相似度是多少呢,edit distance是一个很好的度量,定义从字符串a变到字符串b,所需要的最少的操作步骤(插入,删除,更改)为两个字符串之间的编辑距离。对于eeba,abca它们之间的编辑距离为3,可以按照上面的操作步骤(不是唯一的)将eeba变到abca,1.将e变为a 2.删除e 3.添加c 共3个步骤。典型的动态规划问题。EDIT[i,j]表示对于字符串a从1到i的子串和字符串b从1到j的字串的编辑距离。(字符串下标从1开始)EDIT[i - 1,j] + 1表示对a 在i 位置删除delete操作EDIT[i,j - 1] + 1 表示i 阅读全文
posted @ 2012-12-31 00:04 kkmm 阅读(9552) 评论(0) 推荐(2) 编辑
  2012年12月30日
摘要: 直接用除数去一个一个加,直到被除数被超过的话,会超时。解决办法每次将被除数增加1倍,同时将count也增加一倍,如果超过了被除数,那么用被除数减去当前和再继续本操作。#include <iostream>#include <vector>#include <string>using namespace std;class Solution {public: int divide(int dividend, int divisor) { // Start typing your C/C++ solution below // DO NOT write int 阅读全文
posted @ 2012-12-30 22:49 kkmm 阅读(5455) 评论(1) 推荐(0) 编辑
摘要: /*每次对于当前的字符判断是否属于1-9(0肯定不行,因为0不在1-26中),如果属于,那么当前的字符可以被decode,并且和f[n-1]组合,f[n] += f[n-1]然后对于当前字符和前一个字符组成的字符串判断是否属于10-26,如果属于,那么这两个字符可以被decode,并且和f[n-2]组合,f[n] += f[n-2]*/#include <iostream>#include <vector>#include <string>using namespace std;class Solution {public: int numDecodings 阅读全文
posted @ 2012-12-30 20:54 kkmm 阅读(2119) 评论(0) 推荐(0) 编辑
摘要: class Solution {public: //when see str, return count and say of str string process(const string& str){ if (str == "") return ""; int l = 0; int r = 0; int count = 0; string result = ""; for (; r < str.length(); ){ if (str[r] == st... 阅读全文
posted @ 2012-12-30 20:37 kkmm 阅读(228) 评论(0) 推荐(0) 编辑
摘要: 直来直去的题目,注意用 l和 r 两个下标来指示左右范围,并且用 if ( l > r ) 来作为递归出口,相比于用 start 和 len 来指示范围更优雅且不易出错。class Solution {public: TreeNode *sortedArrayToBST(vector<int> &num, int l, int r){ if (num.size() < 1 || l < 0 || r > num.size()-1 || l > r) return NULL; int mid = (l+r)/2; TreeNo... 阅读全文
posted @ 2012-12-30 17:27 kkmm 阅读(183) 评论(0) 推荐(0) 编辑
摘要: #include <iostream>#include <vector>using namespace std;class Solution {public: int maxArea(vector<int> &height) { // Start typing your C/C++ solution below // DO NOT write int main() function if (height.size() < 2) return 0; int l = 0; int r = height... 阅读全文
posted @ 2012-12-30 17:17 kkmm 阅读(279) 评论(0) 推荐(0) 编辑
上一页 1 ··· 8 9 10 11 12 13 14 15 16 ··· 31 下一页