Special Leetcode dichotomy of -154. Sorted array to find the minimum rotational II (Find Minimum in Rotated Sorted Array II)

Special Leetcode dichotomy of -154. Sorted array to find the minimum rotational II (Find Minimum in Rotated Sorted Array II)


 

Suppose ascending order according to the array was rotated in a previously unknown point.

(For example, the array  [0,1,2,4,5,6,7] may become  [4,5,6,7,0,1,2] ).

Find the smallest elements.

Note that there may be repeated elements in the array.

Example 1:

Input: [1,3,5] 
Output: 1

Example 2:

Input: [2,2,2,0,1] 
Output: 0


With 153 questions like, but this problem will have duplicate numbers, the situation with the code 153 will be given: 3313
This sample is rotated from the intermediate node 1 and 3, then we only need 153 the following modifications can be made on the code:
If nums [mid] == nums [R ] on R--, because they are the same as two, so that the right directly omitted.

class Solution {
    public int findMin(int[] nums) {
        if(nums.length==0 || nums==null) return 0;
        int L = 0;
        int R = nums.length - 1;
        while(L<R){
            int mid = (L+R)>>>1;
            if(nums[mid]>nums[R]){
                L = mid+1;
            }else if(nums[mid]<nums[R]){
                R = mid;
            }else{
                R--;
            }
        }
        return nums[L];
    }
}

 



Guess you like

Origin www.cnblogs.com/qinyuguan/p/11410179.html