不多说 直接上毕老师源码
import java.util.*;
class ArrayTest4
{
public static void main(String[] args)
{
int[] arr = {2,4,5,7,8,19,32,45};//8
int index = getIndex_2(arr,190);
System.out.println("index="+index);
}
public static int getIndex_2(int[] arr,int key)
{
int min = 0,max = arr.length-1,mid;
while(min<=max)
{
mid = (max+min)>>1;
if(key>arr[mid])
min = mid + 1;
else if(key<arr[mid])
max = mid - 1;
else
return mid;
}
return min;
}
对于最后return min 表示 有点小疑惑 金币奉上 求解释 ~~~~
|