上一页 1 ··· 5 6 7 8 9 10 11 12 13 ··· 16 下一页
摘要: 斐波那契数列的变形,用递归会过不了large judge。所以采用另外一种方法。class Solution {public: int climbStairs(int n) { if(n==0)return 1; if(n==1)return 1; int a=1; int b=1; int c; for(int i=2;i<=n;i++) { c=a+b; a=b; b=c; } return c... 阅读全文
posted @ 2013-05-10 22:47 代码改变未来 阅读(231) 评论(0) 推荐(0) 编辑
摘要: 1.二叉树转链表Flatten Binary Tree to Linked ListGiven a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like:1 \ 2 \ 3 \ 4 \ 5 \ ... 阅读全文
posted @ 2013-05-09 22:35 代码改变未来 阅读(204) 评论(0) 推荐(0) 编辑
摘要: 二叉树层序遍历,然后串成一个单链表,不允许用队列,要求空间复杂度为常数struct Node { int value; Node *left; Node *right; Node *next;};Node *Func(Node *root) { if (NULL == root) return NULL; Node *p1 = root, *p2 = root; p2->next = p2->left; if (NULL != p2->left) { p2->left->next = p2->right; els... 阅读全文
posted @ 2013-05-09 14:31 代码改变未来 阅读(236) 评论(0) 推荐(0) 编辑
摘要: 把每个string按字母排个序,然后就能很方便的进行string的两两比较class Solution { private: vector<string> ret; map<string, vector<string> > m; public: vector<string> anagrams(vector<string> &strs) { ret.clear(); m.clear(); for(int i = 0; i < strs.size(); i++) { string sor... 阅读全文
posted @ 2013-05-07 19:09 代码改变未来 阅读(156) 评论(0) 推荐(0) 编辑
摘要: 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 @ 2013-05-04 14:52 代码改变未来 阅读(164) 评论(0) 推荐(0) 编辑
摘要: 4-sum 已通过测试定义一对指针,指向两头。再定义一对指针,指向中间的两个元素。加起来看看跟target比较一下。再决定内部指针怎么移动。class Solution {public: vector<vector<int> > fourSum(vector<int> &num, int target) { vector<int>v; vector<vector<int>>v1; int len=num.size(); if(len<=3)return v1; sort(num.begin(),num.end 阅读全文
posted @ 2013-05-01 23:42 代码改变未来 阅读(246) 评论(0) 推荐(0) 编辑
摘要: 昨天面试微软又考到层次遍历。添上leetcode此题代码class Solution { public: vector<vector<int> > zigzagLevelOrder(TreeNode *root) { vector<vector<int>>result; if(root==NULL)return result; vector<int>levelresult; vector<TreeNode *>v; int last=0; int front=0; int rear=0; TreeNode *p=root; 阅读全文
posted @ 2013-04-24 13:26 代码改变未来 阅读(259) 评论(0) 推荐(0) 编辑
摘要: class Solution {public: int minDistance(string word1, string word2) { int m=word1.size(); int n=word2.size(); if(m==0)return n; if(n==0)return m; int v[501][501]; int i,j; for(i=0;i<=m;i++) { v[i][0]=i; } for(j=0;... 阅读全文
posted @ 2013-04-22 22:23 代码改变未来 阅读(175) 评论(0) 推荐(0) 编辑
摘要: http://blog.csdn.net/rein07/article/details/6741661 阅读全文
posted @ 2013-04-21 15:24 代码改变未来 阅读(94) 评论(0) 推荐(0) 编辑
摘要: class Solution { private: vector<int> f; public: int getF(int index) { if (index < 0) return 1; else return f[index]; } int numDecodings(string s) { // Start typing your C/C++ solution below // DO NOT write int main() f... 阅读全文
posted @ 2013-04-21 15:05 代码改变未来 阅读(92) 评论(0) 推荐(0) 编辑
上一页 1 ··· 5 6 7 8 9 10 11 12 13 ··· 16 下一页