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) 编辑
摘要: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public: TreeNode *buildTree(vector<int> &pre, int preStart, vector<int> &in, int inStart, int len){ if (preStart<0 || preStart+len>pre.size() || 阅读全文
posted @ 2012-12-30 16:55 kkmm 阅读(243) 评论(0) 推荐(0) 编辑
摘要: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public: TreeNode *buildTree(vector<int> &in, int inStart, vector<int> &post, int postStart, int len){ if (inStart<0 || inStart+len>in.size() || 阅读全文
posted @ 2012-12-30 16:50 kkmm 阅读(189) 评论(0) 推荐(0) 编辑
摘要: #include <iostream>#include <stack>using namespace std;struct Node{ int data; Node *next; Node(int d) : data(d) {}};int label = 0;//思路:设两个指针,left和right,首先将right走到头,交换left和right,然后每return一次,right自动向左一次,left手动向右走一次。void Reverse(Node *&left, Node *right){ if (!left || !right) return;... 阅读全文
posted @ 2012-12-30 16:29 kkmm 阅读(499) 评论(0) 推荐(0) 编辑