int left = 0;
int right = nums.length - 1;
int res = -1;
while(left <= right){
//注意这里可以使用 right + (left - right)/2来防止数字溢出
//相同的 right + (left - right) >> 1
int mid = (right + left)/2;
if(nums[mid] == target)res = mid;
else if(nums[mid] > target){
right = mid - 1;
}else{
left = mid + 1;
}
}
return res;
原文:https://www.cnblogs.com/kikochz/p/13296333.html