當(dāng)前位置:首頁(yè) > IT技術(shù) > 編程語(yǔ)言 > 正文

劍指 Offer 11. 旋轉(zhuǎn)數(shù)組的最小數(shù)字
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;
                //無(wú)法判斷最小值在哪個(gè)半?yún)^(qū)
            }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/

開(kāi)通會(huì)員,享受整站包年服務(wù)立即開(kāi)通 >