摘要: 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 ... 阅读全文
posted @ 2015-06-02 23:53 jianchao-li 阅读(185) 评论(0) 推荐(0) 编辑
摘要: 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 ... 阅读全文
posted @ 2015-06-02 23:47 jianchao-li 阅读(199) 评论(0) 推荐(0) 编辑
摘要: 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... 阅读全文
posted @ 2015-06-02 23:42 jianchao-li 阅读(223) 评论(0) 推荐(0) 编辑
摘要: Well, this problem is similar toUnique Paths. The introduction of obstacles only changes the boundary conditions and make some points unreachable (sim... 阅读全文
posted @ 2015-06-02 23:41 jianchao-li 阅读(153) 评论(0) 推荐(0) 编辑
摘要: 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,... 阅读全文
posted @ 2015-06-02 23:40 jianchao-li 阅读(201) 评论(0) 推荐(0) 编辑
摘要: 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... 阅读全文
posted @ 2015-06-02 23:39 jianchao-li 阅读(363) 评论(0) 推荐(0) 编辑
摘要: 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... 阅读全文
posted @ 2015-06-02 23:36 jianchao-li 阅读(170) 评论(0) 推荐(0) 编辑
摘要: 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... 阅读全文
posted @ 2015-06-02 23:31 jianchao-li 阅读(172) 评论(0) 推荐(0) 编辑
摘要: A classic tree traversal problem. I share my two solutions here: BFS and DFS.BFS: 1 vector> levelOrder(TreeNode *root) { 2 vector> levels;... 阅读全文
posted @ 2015-06-02 23:29 jianchao-li 阅读(174) 评论(0) 推荐(0) 编辑
摘要: 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... 阅读全文
posted @ 2015-06-02 23:27 jianchao-li 阅读(247) 评论(0) 推荐(0) 编辑
摘要: 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... 阅读全文
posted @ 2015-06-02 23:22 jianchao-li 阅读(254) 评论(0) 推荐(0) 编辑
摘要: 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... 阅读全文
posted @ 2015-06-02 23:20 jianchao-li 阅读(248) 评论(0) 推荐(0) 编辑
摘要: 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,... 阅读全文
posted @ 2015-06-02 23:18 jianchao-li 阅读(155) 评论(0) 推荐(0) 编辑
摘要: This problem is a little tricky at first glance. However, if you have finished theHouse Robber problem, this problem can simply bedecomposed into two ... 阅读全文
posted @ 2015-06-02 23:17 jianchao-li 阅读(172) 评论(0) 推荐(0) 编辑
摘要: 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... 阅读全文
posted @ 2015-06-02 23:11 jianchao-li 阅读(220) 评论(0) 推荐(0) 编辑
摘要: Well, have you solved thenextPermutationproblem? If so, your code can be used in this problem. The idea is fairly simple:sortnumsin ascending order, a... 阅读全文
posted @ 2015-06-02 23:09 jianchao-li 阅读(248) 评论(0) 推荐(0) 编辑
摘要: 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... 阅读全文
posted @ 2015-06-02 23:06 jianchao-li 阅读(267) 评论(0) 推荐(0) 编辑