摘要:public class Solution { /** * @param A an integer array sorted in ascending order * @param target an integer * @return an integer */ public int totalOccurrence(int[] A, in...
阅读全文
摘要:Method One: Using Binary Search Once. The point is how to calculated the number's index. mtarix[<num> / nCol][<num> % nCol] Method Two:Binary Search T
阅读全文
摘要:O(logN)For the last element, if nums[mid] == target, we threw the first part
阅读全文
摘要:Important point: 1) While (start + 1 < end) 2) int mid = start + (end - start)/2;3) check both nums[start] and nums[end] at the end
阅读全文
摘要:这是一道典型的拓扑问题。所以我们可以根据拓扑模型来完成这道题。这里面我们需要知道每个课程的入度和每个课程修完后可以解锁的课程。在这里需要注意的是因为课程从0到n-1,所以直接用index就可以做Key,不需要用到hash。这道题因为使用了入读,所以不用分级。因为入读已经帮我们把级分好。
阅读全文
摘要:在写BFS的时候,经常会用到queue来标记需要验证的点(即由上一层而关联的各个点,就是所谓的灰色的点。黑色的点是queue最上面的那个点,就是正在被process会被poll出来那个)。然后辅助一个hash来记录是否已经visit过这个node。
阅读全文
摘要:Useful Links for Explanation: http://www.programcreek.com/2012/12/leetcode-clone-graph-java/ http://www.cnblogs.com/springfor/p/3874591.html 这道题可以用BFS
阅读全文
摘要:Very useful basic study material: http://blog.csdn.net/raphealguo/article/details/7523411 1. BFS The hard part is to define the level. Using int size
阅读全文
摘要:Analysis: 1) 左子树是不是有LCA(A,B) 2) 右子树是不是有LCA(A,B) ==> A. 有 返回LCA(A,B) B. 没有 ==> b1) 如果有遇到A或者B, 返回A或B b2)没有遇到返回null 注意;要判断AB有没有遇到。
阅读全文
摘要:这道题很简单,就是中序遍历,得到list然后valid数字排序即可
阅读全文
摘要:Analysis 这个结果是用Divid & Conquer 做的 1. Divid: tree->left child and right child 2. Conquer: when it is null, return 0 3. Merge:如果是sequence, 则现在的sequence的
阅读全文
摘要:Analysis: 这道题不难,但是我对于数结构以及递归掌握还是不够好,无法举一反三。 这道题要求所有的path,可以想到排列组合问题,所以可以套用排列组合模板。 但是因为这个是树结构,所以在套用的时候,要左右子树各走一遍。 这道题也是树的问题,用递归的时候要考虑三要素,递归的定义,递归的拆解,递归
阅读全文
摘要:Good reference for all tree problem https://sites.google.com/site/jennyshelloworld/company-blog/chapter-3 binary-tree-divide-conquer Analysis: Here we
阅读全文
摘要:1. Recursive Straight Forward, left -> right -> root 2. Iterator This is a little bit hard to understand, here is good reference https://sites.google.
阅读全文
摘要:1. Recursive Straight Forward, Add left -> root -> right 2. Iterator 1. Create a node to present current node. 2. If current node is not null and stac
阅读全文
摘要:1. Recursive: Very Straight forward, If the node is not null, add root -> traversal left -> traversal right 2. Iterator(find a very good explanation h
阅读全文
摘要:Notice 1. Input String always need to be check whether it is NULL; 2. For the conner cases, source is shorter than the target or target is empty, it c
阅读全文
摘要:这道题和Palindrome Partitioning很像,都是怎么切数组的问题,唯一需要注意的是ip的规定 1)不能出现0和其他数字组合, 类似0.00.01.1。0只能单独出现或者在一个片段中不为开头的数字。 2)每一个片段数字最大为255 3)只能有四个片段 4)这里需要注意一个细节: 在这里
阅读全文