摘要:
斐波那契数列的变形,用递归会过不了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... 阅读全文
摘要:
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 \ ... 阅读全文
摘要:
二叉树层序遍历,然后串成一个单链表,不允许用队列,要求空间复杂度为常数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... 阅读全文
摘要:
把每个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... 阅读全文
摘要:
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 阅读全文
摘要:
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 阅读全文
摘要:
昨天面试微软又考到层次遍历。添上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; 阅读全文
摘要:
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;... 阅读全文