摘要:class Solution { /** * @param A: sorted integer array A which has m elements, * but size of A is m+n * @param B: sorted integer array B which has n elements * @retu...
阅读全文
摘要:class Solution { /** * @param A and B: sorted integer array A and B. * @return: A new sorted integer array */ public int[] mergeSortedArray(int[] A, int[] B) { // Write...
阅读全文
摘要:Note:a balaced tree needs to be balaced all the time. So it is important remember the status of each subtree. If it is not balaced in the middle, the
阅读全文
摘要:/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left ...
阅读全文
摘要:/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left ...
阅读全文
摘要:This question is a little bit harder than the Max Depth. We only compare the depth of the leaf node. If it is missing one side, this level shouldn't b
阅读全文
摘要:public class Solution { /** * @param A an integer array * @return void */ public void sortIntegers2(int[] A) { // Write your code here if (A == null || A.length...
阅读全文
摘要:O(n) worst, O(nlogn) average. Use quickSort This is quickSort
阅读全文
摘要:Note: 审题: 注意也有一种情况是: 一共有N个元素,要k个,但有不需要从每一个块去取, 只需要从其中的一些取。所以这里end开始应该取最大的element。而且有可能完全取不到,所以start应该从0开始取。剩下的和copy book那道题想法一致。
阅读全文
摘要:Note: 这道题很有意思,用的是二分法。想法是不停的试错。这个博客主把这个问题说的很清楚 https://xuezhashuati.blogspot.com/2017/03/lintcode-437-copy-books.html 其实二分法的方法可以变化的点很有限,最多变化的就是检查条件。一般难
阅读全文
摘要:Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or
阅读全文
摘要:Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return the length of the LIS. Given a sequence of integer
阅读全文
摘要:方法一: 动态规划 https://segmentfault.com/a/1190000003768736 https://siddontang.gitbooks.io/leetcode-solution/content/dynamic_programming/perfect_squares.htm
阅读全文
摘要:Note:From back to start, if A[distance] is larger than the distance to the last element, it indicates that the last element is reachable. We can move
阅读全文
摘要:public class Solution { /** * @param n: An integer * @return: An integer */ public int climbStairs(int n) { // write your code here if (n == 0 || n == 1) { ...
阅读全文
摘要:public class Solution { /** * @param grid: a list of lists of integers. * @return: An integer, minimizes the sum of all numbers along its path */ public int minPathSum(int[][] ...
阅读全文
摘要:Important point:During the iniitialize, the top or left side, if one grid is BLOCK, the rest of those points are all blocked.
阅读全文
摘要:Actually it can initial f[0][0] to 1, so that it can avoid a lot of coner checking.
阅读全文
摘要:Important: The hardest part is to define the state(here is f[x][y]) and its function(the relationship between them)
阅读全文
摘要:Important Note:1) Pay attention to the number that is less than 1.0, because x^0.5 will larger than x. So we need to always set the right side to be 1
阅读全文
摘要:Important Reference: http://stackoverflow.com/questions/13093602/finding-subarray-with-maximum-sum-number-of-elements http://www.jiuzhang.com/qa/2942/
阅读全文
摘要:The reference link: http://www.code123.cc/docs/leetcode-notes/binary_search/search_a_2d_matrix_ii.html 题解 - 自右上而左下
阅读全文
摘要:This question is very hard, just remember it:Explaination: 思路清晰,就是二倍法。直接用除数去一个一个加,直到被除数被超过的话,会超时。解决办法每次将被除数增加1倍,同时将count也增加一倍,如果超过了被除数,那么用被除数减去当前和再继续本
阅读全文
摘要:Important Node: 1) This problem has two parts: a. find the closest number's to the target and return its index. This is standard binary search problem
阅读全文
摘要:The worst situation O(N). Actually we can either just loop through, or we can compare num[mid] with the num[end], if they are the same, that means it'
阅读全文
摘要:O(logN) This question turns to find the first and last element of the target in a sorted array. Just be careful with the two result coming out of the
阅读全文
摘要:O(N) if element can repeat, the worst case, you cannot throw away any section. eg. [1, 1, 1, 1, 0, 1, 1] target = 0, you cannot throw any section. We
阅读全文
摘要:O(logN) Important Point: Once the target is in one section, use the point in that section as benchmark In this problem, if the target >= startVal, use
阅读全文
摘要:O(logN) Check four different situations: 1) The peek 2) increasing section 3) decreasing section 4) minimum
阅读全文
摘要:O(logN) There are two section: 1) increase array which the first element is larger than end one. 2) the minimum to end Comparing with the endVal, 1) i
阅读全文
摘要:O(logN) Important Point: if there "mid" exists, it has at laest three elements in "nums", because otherwise it will not come inside the loop. So later
阅读全文
摘要:class Solution { /** * @param x: An integer * @return: The sqrt of x */ public int sqrt(int x) { // write your code here if (x < 0) { return -1; ...
阅读全文
摘要:If it is two eggs: http://datagenetics.com/blog/july22012/index.html Imagine we drop our first egg from floor n, if it breaks, we can step through the
阅读全文
摘要: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
阅读全文