摘要:
This is pretty simple.Just use three element to pretain the result; 1 class Solution { 2 public: 3 int climbStairs(int n) { 4 vector dp(3,... 阅读全文
摘要:
Two notes:1. result at least n candys. Use array scan from beginning and from end.2. dp[i] = max(dp[i], count++). count++ already record the increasin... 阅读全文
摘要:
Made a stupid bug....... When reverse the vector, stop it at mid. Otherwise, it will swap back......Mark!!!!!!!! 1 /** 2 * Definition for binary tree... 阅读全文
摘要:
Recursive method can be unstand easily:1. Assume we get the sub node with this transition. So we need to make the current node.2. As the symmetic, the... 阅读全文
摘要:
Same with inorder.1. recursive: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeN... 阅读全文
摘要:
Still 3 methods:Same with inorder, but post order is a little different. We need to treat it as reverse result of preorder.So we go to right branch fi... 阅读全文
摘要:
Use lMax get the maximum value of a sub SINGLE branch (left branch or right branch or just current node).Use gMax get the maximum value of the whole s... 阅读全文
摘要:
This question is almost same as the previous one.I just use a stack (another vector) of vector to store the level result. Then reverse it. 1 /** 2 * ... 阅读全文
摘要:
Basic question. Use a queue to store Node, use two numbers to record current level node numbers and next level node numbers. 1 /** 2 * Definition for... 阅读全文
摘要:
There are three methods to do it:1. recursive(use memory stack): (Time O(n), Space O(logn) 1 /** 2 * Definition for binary tree 3 * struct TreeNode ... 阅读全文
摘要:
It is similar to use stack for BST inorder traversal.So do the same thing :1. Get stack to store right branchs(include current node).2. When you pop t... 阅读全文
摘要:
This is combination of II and III.1. when k >= length, it is totally II.2. when k localMax[j] = max(localMax[j] + diff, globalMax[j-1] + max(0, diff... 阅读全文
摘要:
III is a kind of I. But it require 2 maximum value. So scan from begin and scan from end to record the maximum value for currrent index.Then scan the ... 阅读全文
摘要:
This is simple, use greedy algorithm will solve the problem. 1 class Solution { 2 public: 3 int maxProfit(vector &prices) { 4 int len = pr... 阅读全文
摘要:
1 class Solution { 2 public: 3 int maxProfit(vector &prices) { 4 if (prices.size() < 2) return 0; 5 int len = prices.size(), resu... 阅读全文
摘要:
Need a helper function to return the binary tree maximum height. If the return value is -1, it means there is a imbalanced subbranch . 1 /** 2 * Defi... 阅读全文
摘要:
Use a hash table to record it. The tricky part is that when the hash value is > 0, it is the index + 1. Otherwise, that string already exists in resul... 阅读全文
摘要:
Same with add binary. You can also skip delete the result pointer. But just return result->next. Depends on the interviewer. 1 /** 2 * Definition for... 阅读全文
摘要:
This question is pretty straight forward. 1 class Solution { 2 public: 3 string addBinary(string a, string b) { 4 int runnerA = a.size()-1... 阅读全文
摘要:
To be honest, I dont like this problem. This is just an extra layer loop for 3sum. But two mistakes I had made…..1. when skip j, use condition j > i+1... 阅读全文