摘要:
This problem seems to be tricky at first glance. However, if you know Morris traversal, it is just the preorder case of Morris traversal and the code ... 阅读全文
摘要:
There are many merge-sort solutions at the forum, but very few quicksort solutions. So I post my accepted quicksort solution here.Well, after reading ... 阅读全文
摘要:
This is a typical DP problem. Suppose the minimum path sum of arriving at point(i, j)isS[i][j], then the state equation isS[i][j] = min(S[i - 1][j], S... 阅读全文
摘要:
Well, this problem is similar toUnique Paths. The introduction of obstacles only changes the boundary conditions and make some points unreachable (sim... 阅读全文
摘要:
This is a fundamental DP problem. First of all, let's make some observations.Since the robot can only move right and down, when it arrives at a point,... 阅读全文
摘要:
Well, this problem has a naive solution, which is to sort the array in descending order and return thek-1-th element. However, sorting algorithm gives... 阅读全文
摘要:
To be honest, this problem is designed to let you use stacks. However, I don't. In fact, you only need to keep a flagand switch it between falseandtru... 阅读全文
摘要:
Well, I do not see what this problem is for. The same code of Binary Tree Level Order Traversal can be used here. The only difference is that we shoul... 阅读全文
摘要:
A classic tree traversal problem. I share my two solutions here: BFS and DFS.BFS: 1 vector> levelOrder(TreeNode *root) { 2 vector> levels;... 阅读全文
摘要:
This is a fundamental and yet classic problem. I share my three solutions here:Iterative solution using stack ---O(n)time andO(n)space;Recursive solut... 阅读全文
摘要:
This is a fundamental and yet classic problem. I share my three solutions here:Iterative solution using stack ---O(n)time andO(n)space;Recursive solut... 阅读全文
摘要:
This is a fundamental and yet classic problem. I share my three solutions here:Iterative solution using stack ---O(n)time andO(n)space;Recursive solut... 阅读全文
摘要:
Well, the basic idea is fairly straightforward. We maintain a mappingmpfrom a value innumsto its position (index)i. Each time we meet an unseen value,... 阅读全文
摘要:
This problem is a little tricky at first glance. However, if you have finished theHouse Robber problem, this problem can simply bedecomposed into two ... 阅读全文
摘要:
Well, have you solved thenextPermutationproblem? If so and you have handled the cases of duplicates at that problem, your code can be used in this pro... 阅读全文
摘要:
Well, have you solved thenextPermutationproblem? If so, your code can be used in this problem. The idea is fairly simple:sortnumsin ascending order, a... 阅读全文
摘要:
Well, in fact the problem of next permutation has been studied long ago. From theWikipedia page, in the 14th century, a man named Narayana Pandita giv... 阅读全文