题目:
解题思路:
在循环中用 nums[mid] 与target比较,
-
若 nums[mid] < target , 则范围向大于 mid一侧收缩
-
若 nums[mid] > target , 则范围向小于 mid一侧收缩
int search(int* nums, int numsSize, int target) {
int l = 0, r = numsSize -1;
while (l <= r) {
int mid = (r + l) / 2;
if (target > nums[mid]) {
l = mid + 1;
}
else if (target < nums[mid]) {
r = mid - 1;
}
else return mid;
}
return -1;
}