- 博客(198)
- 收藏
- 关注
原创 leetcode-255-Verify Preorder Sequence in Binary Search Tree
cannot solve it.use the property of BST and preorder, which is the first element is the root of the tree, we can break it into 2 part, and the upper bound of left part is root, so as the lower bound ...
2019-04-02 17:33:57
190
原创 leecode-773-Sliding Puzzle
cannot solve it.Simply use BFS, and use status compress to compress the tiles into a string, and use set to record it.
2019-04-02 12:02:42
263
原创 leetcode-29-Divide Two Integers
error:cannot solve it.pattern:check edge case: if up == INT_MIN, return INT_MAX if down == -1, INT_MIN if down == 1;convert up and down to positive integerevery time use a variable to remember h...
2019-02-21 14:58:41
224
原创 973-K Closest Points to Origin
O(nlogk), use priority_queue to do this,create a max heapif(heap.size() < K or heap.top() > cur_dis), then update new data.
2019-02-21 14:23:04
282
原创 leetcode-348-Design Tic-Tac-Toe
error:do not know what is move means,use row array, col array, diag, anti_diag to record current situation. Assign 1 if player is 1, and -1 if player is 2
2019-02-21 12:51:48
223
原创 leetcode-583-Delete Operation for Two Strings
error:cannot solve it.pattern:dp[i][j] = dp[i - 1][j - 1] + 1 (if s[i - 1] == p[j - 1])dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) (if s[i - 1] != p[j - 1])now cannot explain why dp[i][j] = dp[i ...
2019-02-21 10:02:50
111
原创 leetcode-329-Longest Increasing Path in a Matrix
error:cannot solve it.pattern:dfs(x, y) = max(1 + dfs(new_x, new_y)), where val[x][y] < val[new_x][new_y].if(dp[x][y] != -1) return dp[x][y];
2019-02-21 07:53:16
143
原创 leetcode-162-Find Peak Element
error:cannot solve it.it is about two part of array, not two point, every time compare rightmost of left part and leftmost of right part, i.e. nums[mid] and nums[mid + 1]. If nums[mid] &lt; nums[mid...
2019-02-18 14:33:52
168
原创 leetcode-560-Subarray Sum Equals K
error:cannot solve it.assume T[i, j] = k, where T[i, j] is the sum of nums[i] to nums[j]. So the equations can transform to: S[j] - S[i - 1] = k. And if we know that preious the occurs of S[j] - k, ...
2019-02-18 09:48:16
154
原创 leetcode-426-Convert Binary Search Tree to Sorted Doubly Linked List
error:cannot solve it.use a previous node to store last inorder variable, then perform inroder traverse. pattern: inorder(root->left); prev->left = root; root->right = prev; ...
2019-02-18 08:24:55
207
原创 leetcode-109-Convert Sorted List to Binary Search Tree
error:cannot solve it.pattern:left open, right close for current search rangeconvert(head, tail): if(head == tail) return nullptr; mid = find(head, tail); TreeNode *tree_mid = new TreeNo...
2019-02-18 08:02:01
120
原创 leetcode-114-Flatten Binary Tree to Linked List
error:cannot slove it.recursive call flatten(root->left), flatten(root->right)get the rightmost of root->leftappend root->right to rightmost of root->leftassign root->left = nu...
2019-02-18 07:08:19
148
原创 Calculate IoU(intersection over union)
assume for each coordinate: {x1, y1}: lower left, {x2, y2}: upper rightinter_x1 = max(x1, x1)inter_y1 = max(y1, y1)inter_x2 = min(x2, x2)inter_y2 = min(y2, y2)then we can calculate the intersecti...
2019-02-17 08:51:41
422
原创 leetcode-295-Find Median from Data Stream
error:cannot slove it.Use two heap, first the keep store left half and other for right heap. And 0 <= |left.size() - right| <= 1. If violent the rule, move to top number to other side. If cu...
2019-02-16 10:04:09
214
原创 leetcode-44-Wildcard Matching
error:use dp equation:dp[i][j]: whether string s of first i characters is match with string p of first j charactersinitial: dp[0][0] = true; dp[0][j] = dp[0][j - 1] (if p[j - 1] == '*')transfo...
2019-02-16 00:23:31
172
原创 leetcode-221-Maximal Square
error:do not want to coding ANYMORE!!!compute the matrix from (0, 0) to (x, y), then get the matrix sums[][].for each point (x, y), compute its possible square, i.e., square from (x, y) to (x + di...
2019-02-15 14:15:16
155
原创 leetcode-410-Split Array Largest Sum
error:use equation: dp[i][j] = min(max(sums[i] - sums[k], dp[k][j - 1])), where dp[i][j] means the min. number divide 0-i into j group.if(j == 1) -> dp[i][j] = sums[i];if(i < j) -> no solu...
2019-02-15 13:55:40
256
原创 leetcode-714-Best Time to Buy and Sell Stock with Transaction Fee
error:use DP. In i-th day, we can buy or sell, so we have two status: buy[i] or sell[i]. Each stands for the max profit if we buy/sell. So:sell[i] = max(sell[i - 1], sell[i - 1] + prices[i] + fee)b...
2019-02-15 06:38:55
103
原创 leetcode-377-Combination Sum IV
error:cannot solve it. DP equation:dp[i] = sum(num + dp[i - num]), where num is a number of nums vector, dp[i] is the total solution of sum to i.Also, initial dp[0] = 1So that we can use memorize ...
2019-02-15 05:35:32
164
原创 leetcode-380-Insert Delete GetRandom O(1)
Error:need to use hashtable + vector.Hashtable to check whether a element is in vector or not. And store the index of vector, i.e., key: val in vector, val: index in vectorVector is use to insert/d...
2019-02-15 04:36:53
146
原创 leetcode-80-Remove Duplicates from Sorted Array II
Use two variable to store last two elements, and check these with current element.
2019-02-14 00:42:49
98
原创 leetcode-274-H-Index
Error:use bucket sort or mapping intuition, assign 0-n array, and increase element of its bucket. Finally, accumulate and get the result.
2019-02-14 00:42:36
168
原创 leetcode-265-Paint House II
Error:first I get the equation: dp[i][j] = min(dp[i - 1][k]) + cost[i][j] (k != j), but it needs O(n * k * k), which loop for all n and for each n loop for all previous k for each current k in the n....
2019-02-14 00:40:27
168
原创 leetcode-647-Palindromic Substrings
Error:I thought it is DP, but I do not it is such stright forward: it just keep track how many Palindromic. The pattern is:for(i = n - 1; i &gt;= 0; i--)for(j = i; j &lt; n; j++) dp[i][j] = s[i] =...
2019-02-13 14:26:13
84
原创 leetcode-334-Increasing Triplet Subsequence
Error:The idea behind it is a little different. The solution is use two variable to traverse, but I use 3 size vector.Just loop the element, store the smaller one to vector, or push a bigger one the...
2019-02-13 14:06:52
86
原创 leetcode-117-Populating Next Right Pointers in Each Node II
Error:misleading by the problem, it said stack cannot count as extra space. In here, we should use iterate solution.Assume we create the next link levelkeep track of next level head and next level...
2019-02-13 13:45:41
98
原创 leecode-90-Subsets II
I think I did it before. Sort the array, then every time push current vector, and avoid duplicate, i.e. skip nums[i] == nums[i - 1].
2019-02-13 12:23:27
123
原创 leetcode-128-Longest Consecutive Sequence
Use mapping to map a sort array to unsort array.Error:First I used min and max to iterate the num, but here it is TLE.iterate element in set, only handle when meet the element is the begin.
2019-02-13 08:49:00
145
原创 leecode-234-Palindrome Linked List
error:cannot solve it.use slow fast pointer to keep track the list. I.e., move slow = slow->next; fast = fast->next->next;reverse second halfcompare it.
2019-02-13 05:59:43
114
原创 leetcode-653-Two Sum IV - Input is a BST
Useless question, which it do not show the values of BST. I thought it should find the answer in BST. But it just use inorder then find it as normal two sum.
2019-02-13 04:45:04
154
原创 leetcode-168-Excel Sheet Column Title
Error:cannot solve it. Pattern:x -= 1;cur = x % 26;x /= 26;which is the same as the normal integer divide, but we need minus one before. So stupid.
2019-02-13 04:38:05
217
原创 leetcode-572-Subtree of Another Tree
It like the check pattern, but the different is here is tree. So1. use preorder(or inorder/postorder) to traverse the tree s2. when meet the same node val, check tree s and t3. pattern for traverse...
2019-02-13 04:24:39
81
原创 leetcode-404-Sum of Left Leave
Use the tree pattern. In here, the pattern is:left_sum(root, is_left): if(root == nullptr) return 0; if(root->left == nullptr && root->right == nullptr && is_left) return ...
2019-02-12 16:14:17
90
原创 leetcode-461-Hamming Distance
Use bit manipulation, compare it bit-wise, then move 1 bit for each.
2019-02-12 15:58:45
167
原创 leetcode-69-Sqrt(x)
Error:cannot solve it. It is binary search, but it also do not depend on left and right, it is not a min. or max. problem. So it cannot directly return left, we should check if current number is in t...
2019-02-12 15:54:22
183
原创 leetcode-235. Lowest Common Ancestor of a Binary Search Tree
Error:Since it is BST, once again, I misunderstand the question. It need to use the property of BST:if(root->val > p->val && root->val > q->val) return common(root->lef...
2019-02-12 15:35:20
134
原创 leetcode-71-Simplify Path
Handle the string, messy but not too difficult. Basically meet “.” then do nothing, meet “…” pop last level. Otherwise append the result string
2019-02-12 14:38:14
134
原创 leetcode-525-Contiguous Array
Error:cannot solve it. Change 0 to -1, so that it become a zero center problem.Assume s[i] = x, and s[j] = x, so that: s[i] + n[i + 1] … + n[j] =s[j] i.e. n[i + 1] + … + n[j] = 0Also, if s[j] =...
2019-02-12 14:00:26
119
原创 leetcod-261-Graph Valid Tree
Error:cannot solve it. So stupid.Use dfs to solve it. Actually we can use BFS, which is simpler. But I think dfs is something we use mostly. In dfs, iterate all node, then count current node and nex...
2019-02-12 13:17:36
95
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人