摘要:
Q:Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path.Note:You can only move either down or right at any point in time.A: DP int minPathSum(vector > &grid) { // Note: The Solution object is instantia... 阅读全文
摘要:
递归。虽然有很多重叠的子问题,但是用DP的话很难表示。。所以用递归也ok,数据集也能过 vector generateTrees(int n) { return construct(1,n); } vector construct(int start,int end) { vector res; if(start>end) { res.push_back(NULL); return res; } for(int i... 阅读全文
摘要:
void swap(int* a, int* b){ int tmp = *a; *a = *b; *b = tmp;}class Solution {public: int removeDuplicates(int A[], int n) { // Note: The Solution object is instantiated only once and is reused by each test case. int cnt = 0; int cur = -1,i; for(i=0;i<n;i++) ... 阅读全文
摘要:
void swap(int* a, int* b){ int tmp = *a; *a = *b; *b = tmp;}class Solution {public: int removeDuplicates(int A[], int n) { // Note: The Solution object is instantiated only once and is reused by each test case. int cur = -1,i; for(i=0;i<n;i++) { if... 阅读全文
摘要:
Catalan number.但是注意Catalan number n==0时, h(n) = 1;而题目中n==0时,cnt =0 int numTrees(int n) { // Note: The Solution object is instantiated only once and is reused by each test case. //Catanlan number //h(n)=h(n-1)*(4*n-2)/(n+1) = C(2n,n)/(n+1) = c(2n,n)-c(2n,n+1)(n=0,1,2,...) ... 阅读全文
摘要:
回溯 vector visited; int m,n; bool exist(vector > &board, string word) { // Start typing your C/C++ solution below // DO NOT write int main() function if(word.empty()) return true; m = board.size(); n = board[0].size(); visited.clear(); ... 阅读全文
摘要:
DP vector grayCode(int n) { // Start typing your C/C++ solution below // DO NOT write int main() function vector res; if(n=0;j--) res.push_back(added+res[j]); cnt *= 2; } return res; } 阅读全文
摘要:
C(n,k) 回溯 vector > combine(int n, int k) { // Note: The Solution object is instantiated only once and is reused by each test case. vector> res; vector path; construct(1,n,k,path,res); return res; } bool construct(int pos,int n,int k,vector& pat... 阅读全文
摘要:
int minDistance(string word1, string word2) { // Note: The Solution object is instantiated only once and is reused by each test case. int m = word1.size(); int n = word2.size(); vector> dp(m+1,vector(n+1,0)); int i,j; for(j=0;j<=n;j++) ... 阅读全文
摘要:
Q:Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?A: 如果交换了,inorder seq中的逆序对可能是1对(父子节点交换),或者2对。 void recoverTree(TreeNode *root)... 阅读全文
摘要:
//DP bool isInterleave(string s1, string s2, string s3) { // Note: The Solution object is instantiated only once and is reused by each test case. //DP if(s1.empty()&&s2.empty()&&s3.empty()) return true; int m = s1.size(); int n = s2.size(); i... 阅读全文
摘要:
vector > zigzagLevelOrder(TreeNode *root) { // Note: The Solution object is instantiated only once and is reused by each test case. vector> res; if(!root) return res; int cnt=1; int tmp; vector levelres; bool bForward = true; que... 阅读全文
摘要:
bool isSymmetric(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(!root) return true; return helper(root->left,root->right); } bool helper(TreeNode* root1, TreeNode* root2) { if(!root1&&!... 阅读全文
摘要:
bottom up. TreeNode *sortedListToBST(ListNode *head) { // Note: The Solution object is instantiated only once and is reused by each test case. //1. getLength if(!head) return NULL; int len = 0; ListNode* cur = head; while(cur) { ... 阅读全文