154. Find Minimum in Rotated Sorted Array II

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Find the minimum element.

The array may contain duplicates.

一刷

public class Solution {
    public int findMin(int[] nums) {
        int lo = 0, hi = nums.length-1;
        while(lonums[hi]){//left half is sorted
                lo = mid+1;
            }
            else if(nums[mid]

二刷
由于是Rotated Sorted Array, 并且最低点在lo和hi之间,那么lo和hi之间只可能是


154. Find Minimum in Rotated Sorted Array II_第1张图片

所以当noms[lo] 当nums[mid]==nums[lo]的时候,并没有其它信息,只能将lo++, 因为lo和mid相等,可以缩短一个查找范围(mid仍在范围中)

public class Solution {
    public int findMin(int[] nums) {
        int lo = 0, hi = nums.length-1;
        while(lonums[lo]){//on the right side
                lo = mid+1;
            }
            else if(nums[mid]

你可能感兴趣的:(154. Find Minimum in Rotated Sorted Array II)