/**
*调用erfen方法,输入数据int[] s= {0,1,2,3,4,5}和8,输出方法的返回值
*/
public class Erfen {
public int search(int[] nums, int target) {
int l=0;
int r=nums.length-1;
while(l<=r) {
int m=(l+r)/2;
if(nums[m]==target)
return m;
if(nums[m]>target)
r=m-1; //左区间
else
l=m+1;//右区间
}
return -1;
}
public static void main(String[] args) {
Erfen ee =new Erfen();
int[] s= {0,1,2,3,4,5};
int ss=8;
System.out.println(ee.search(s, ss));
}
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/java/282955.html