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) 编辑