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/