当前位置:首页 > IT技术 > 编程语言 > 正文

剑指 Offer 11. 旋转数组的最小数字
2022-03-06 18:04:53

package leetcode;

public class offer_11 {
    
    public int minArray(int[] numbers) {
        int left=0;
        int right=numbers.length-1;
        int mid=0;
        while(left<right) {
            mid=(left+right)/2;
            //numbers[mid]>numbers[right],则值只可能在mid与right之间
            if(numbers[mid]>numbers[right]) {
                left=mid+1;
                //numbers[mid]<numbers[right],则值只可能在mid与left之间
            }if(numbers[mid]<numbers[right]) {
                right=mid;
                //无法判断最小值在哪个半区
            }else if (numbers[mid]==numbers[right]) {
                right=right-1;
            }
        }
        
        return numbers[left];
        
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        offer_11 off=new offer_11();
        int[] numbers= {3,1,3};
        System.out.println(off.minArray(numbers));
        }

}

 

本文摘自 :https://www.cnblogs.com/

开通会员,享受整站包年服务立即开通 >