Leetcode code reuse plate _ Divide and Conquer Related

 Minute root treatment

1. The binary search (time complexity of O (log n-) )

Output: If = X A [J], the output j, otherwise the output of 0. The
      1.binarysearch (. 1 , n-)
Process: binarysearch (low, high)
        
        1.if low>high then return 0
        2.else
        3.        mid←(low+high)/2
        4.        if x=A[mid]     then return mid
        5.        else if x<A[mid]  then return binarysearch(low,mid-1)
        6.        else return  binarysearch(mid+1,high)
        7.end if

Leetcode NO33   search rotating array

class Solution {
    public int search(int[] nums, int target) {
        int lo = 0;
        int hi = nums.length - 1;

        the while (LO < Hi) {
             int MID = (LO + Hi) / 2 ;
             // if [0, mid] When ordered, the Statute rearwardly condition 
            IF (the nums [0] <= the nums [MID] && (target> the nums [MID] || target <the nums [0 ])) {
                LO = MID +. 1 ;
                 // When [0, mid] rotated backward condition statute 
            } the else  IF (target> the nums [MID] && target <the nums [0 ]) {
                lo = mid + 1;
            } else {
                hi = mid;
            }
        }
        return lo == hi && nums[lo] == target ? lo : -1;
    }
}

 

 

Guess you like

Origin www.cnblogs.com/samanian/p/11770072.html