int search(int[] nums, int target) {
int high,low = 0;
// 排序倒数
high = nums.length-1;
if(high == 0) {
return -1;
}
while (low<=high) {
int temp = (high+low)/2;
if(target == nums[temp]) {
return temp;
}
else if(target > nums[temp]) {
low = temp + 1;
}else {
high = temp-1;
}
}
return -1;
}
比较简单,但是还是错了一次,如果提前判断high是否为1,返回-1会错,大概是测试用例中有一个长度为2的nums