2014年4月7日

摘要: Problem link:http://oj.leetcode.com/problems/clone-graph/This problem is very similar to "Copy List with Random Pointer", we need a hash map to map a node and its clone.The algorithm is 2-pass procedure as follows.CLONE-GRAPH(GraphNode node): Let MAP be a hash map with pairs of (key=GraphN 阅读全文

posted @ 2014-04-07 01:42 卢泽尔 阅读(274) 评论(0) 推荐(0)

2014年4月6日

摘要: Problem link:http://oj.leetcode.com/problems/candy/Suppose we are given an array R[1..N] that are ratings of N children.Let C[1..N] be a new array where C[i] is the number of candies for i-th child.From the description, C[i] should meet following requirements:LEAST. C[i] is at least 1, for i = 1, .. 阅读全文

posted @ 2014-04-06 23:30 卢泽尔 阅读(288) 评论(0) 推荐(0)

摘要: Problem link:http://oj.leetcode.com/problems/copy-list-with-random-pointer/Deepcopy a linked list with random pointer, realy simple problem, solved in three steps using a hash map:Create a new head, duplicating the head (return NULL for case head==NULL), map key=p1 with value=p2.Let p1 point to head 阅读全文

posted @ 2014-04-06 12:03 卢泽尔 阅读(205) 评论(0) 推荐(0)

摘要: Problem link:http://oj.leetcode.com/problems/word-break-ii/This problem is some extension of the word break problem, so the solution is based on the discussion in Word Break.We also use DP to solve the problem. In this solution, A[i] is not a boolean any more, but a list of all possible value of j&g 阅读全文

posted @ 2014-04-06 11:30 卢泽尔 阅读(587) 评论(0) 推荐(0)

摘要: Problem link:http://oj.leetcode.com/problems/word-break/We solve this problem using Dynamic Programming method. Let A[0..n-1] be a boolean array, where A[i]=True if and only if s[i..n-1] can be segmented into words. The recursive formula is:A[i] = True, if s[i..n-1] is a wordA[i] = True, if there ex 阅读全文

posted @ 2014-04-06 10:46 卢泽尔 阅读(654) 评论(0) 推荐(0)

2014年4月5日

摘要: Problem link:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/According to the wiki page, the algorithm for evaluating any postfix expression is fairly straightforward. The following is the pseudo-code for evaluating a postfix expression.while there are input tokens left read the... 阅读全文

posted @ 2014-04-05 00:02 卢泽尔 阅读(168) 评论(0) 推荐(0)

2014年4月4日

摘要: Problem link:http://oj.leetcode.com/problems/linked-list-cycle-ii/The solution has two step:Detecting the loop using faster/slower pointers.Finding the begining of the loop. After two pointers meet in step 1, keep one pointer and set anther to the head. Let the two pointers both go one step for each 阅读全文

posted @ 2014-04-04 23:48 卢泽尔 阅读(181) 评论(0) 推荐(0)

摘要: Problem link:http://oj.leetcode.com/problems/linked-list-cycle/We set two pointers: the faster pointer goes two steps each iteration, and the slower one goes one step.If the two pointers meet some time, it follows that there is a loop; otherwise, the faster pointer will touch the end of the singly l 阅读全文

posted @ 2014-04-04 23:38 卢泽尔 阅读(185) 评论(0) 推荐(0)

摘要: Problem link:http://oj.leetcode.com/problems/reorder-list/I think this problem should be a difficult problem, since it requries three classic algorithms on the single-linked list:Split a single-linked list into halvesReverse a single-linked listCombine two single-linked list into one alternatelySpli 阅读全文

posted @ 2014-04-04 07:35 卢泽尔 阅读(222) 评论(0) 推荐(0)

摘要: Problem Link:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/Even iterative solution is easy, just use a stack storing the nodes not visited. Each iteration, pop a node and visited it, then push its right child and then left child into stack if possible.Note the special case that the 阅读全文

posted @ 2014-04-04 02:03 卢泽尔 阅读(230) 评论(0) 推荐(0)