摘要:
回溯 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) { ... 阅读全文