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

劍指 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;
                //無法判斷最小值在哪個半?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/

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