2 Binary Search & LogN Algorithm Apr 18

38. Search a 2D Matrix II

https://www.lintcode.com/problem/search-a-2d-matrix-ii/description?_from=ladder&&fromId=1

这道题与二分法有什么关系呢?

  -把整个二维数组从对角线分成两半,从左下角开始,往右上角逼近。

 1 public class Solution {
 2     /**
 3      * @param matrix: A list of lists of integers
 4      * @param target: An integer you want to search in matrix
 5      * @return: An integer indicate the total occurrence of target in the given matrix
 6      */
 7     public int searchMatrix(int[][] matrix, int target) {
 8         // write your code here
 9         if(matrix == null || matrix.length == 0 || matrix[0].length == 0) {
10             return 0;
11         }
12         int row = matrix.length - 1, col = matrix[0].length - 1;
13         int r = row, c = 0;
14         int result = 0;
15         while(r >= 0 && c <= col) {
16             if(matrix[r][c] == target) {
17                 result++;
18                 c++;
19                 r--;
20             } else if(matrix[r][c] < target) {
21                 c++;
22             } else if(matrix[r][c] > target) {
23                 r--;
24             }
25         }
26         return result;
27     }
28 }

457. Classical Binary Search

https://www.lintcode.com/problem/classical-binary-search/description?_from=ladder&&fromId=1

很简单,套用模版即可

 1 public class Solution {
 2     /**
 3      * @param nums: An integer array sorted in ascending order
 4      * @param target: An integer
 5      * @return: An integer
 6      */
 7     public int findPosition(int[] nums, int target) {
 8         // write your code here
 9         if(nums == null || nums.length == 0) return -1;
10         int start = 0, end = nums.length - 1;
11         while(start + 1 < end) {
12             int mid = start + (end - start) / 2;
13             if(nums[mid] == target) return mid;
14             if(nums[mid] < target) {
15                 start = mid;
16             } else {
17                 end = mid;
18             }
19         }
20         if(nums[start] == target) {
21             return start;
22         } else if(nums[end] == target) {
23             return end;
24         } 
25         return -1;
26     }
27 }

141. Sqrt(x)

https://www.lintcode.com/problem/sqrtx/description?_from=ladder&&fromId=1

凡是 平方、移位,一定要类型转换!!!转换成 long !!!

套用模版即可

 1 public class Solution {
 2     /**
 3      * @param x: An integer
 4      * @return: The sqrt of x
 5      */
 6     public int sqrt(int x) {
 7         // write your code here
 8         if(x < 0) return -1;
 9         long start = 0; 
10         long end = x;
11         long xl = (long)x;
12         while(start + 1 < end) {
13             long mid = start + (end - start) / 2;
14             if(mid * mid == xl) {
15                 return (int)(mid);
16             } else if(mid * mid > xl) {
17                 end = mid;
18             } else {
19                 start = mid;
20             }
21         }
22         if(end * end <= xl) {
23             return (int)(end);
24         } else {
25             return (int)(start);
26         }
27     }
28 }

617.  Maximum Average Subarray

https://www.lintcode.com/problem/maximum-average-subarray-ii/note/171478

586. Sqrt(x) II

https://www.lintcode.com/problem/sqrtx-ii/description?_from=ladder&&fromId=1

牛顿迭代法。

public class Solution {
    /**
     * @param x: a double
     * @return: the square root of x
     */
    public double sqrt(double x) {
        // write your code here
        double result = 1.0;
        while(Math.abs(x - result * result) > 1e-12) {
            result = (result + x / result) / 2;
        }
        return result;
    }
}

160. Find Minimum in Rotated Array II

https://www.lintcode.com/problem/find-minimum-in-rotated-sorted-array-ii/description?_from=ladder&&fromId=1

要知道最坏情况下,[1, 1, 1, .........., 1] 里有一个 0

这种情况使得时间复杂度必须是 O(n)

if mid equals to end, that means it's fine to remove end

the smallest element won't be removed

public class Solution {
    /**
     * @param nums: a rotated sorted array
     * @return: the minimum number in the array
     */
    public int findMin(int[] nums) {
        // write your code here
        if(nums == null || nums.length == 0) return 0;
        int start = 0;
        int end = nums.length - 1;
        while(start + 1 < end) {
            int mid = start + (end - start) / 2;
            if(nums[mid] == nums[end]) {
                end--;
            }
            if(nums[mid] > nums[end]) {
                start = mid;
            }
            if(nums[mid] < nums[end]) {
                end = mid;
            }
        }
        return Math.min(nums[start], nums[end]);
    }
}

63. Search in Rotated Sorted Array II

https://www.lintcode.com/problem/search-in-rotated-sorted-array-ii/description?_from=ladder&&fromId=1

 1 class Solution {
 2     public boolean search(int[] nums, int target) {
 3         if(nums == null || nums.length == 0) return false;
 4         int start = 0;
 5         int end = nums.length - 1;
 6         int mid;
 7         while(start + 1 < end){
 8             mid = start + (end - start) / 2;
 9             if(nums[mid] == target) return true;
10             if(nums[mid] > nums[start]){
11                 if(target >= nums[start] && target <= nums[mid]){
12                     end = mid;
13                 }else{
14                     start = mid;
15                 }
16             }else if(nums[mid] < nums[start]){
17                 if(target >= nums[mid] && target <= nums[end]){
18                     start = mid;
19                 }else{
20                     end = mid;
21                 }
22             }
23             else{
24                 start++;
25             }
26         }
27         if(target == nums[start] || nums[end] == target) return true;
28         return false;
29     }
30 }
原文地址:https://www.cnblogs.com/jenna/p/10733632.html