使用左闭右闭的写法
r = n - 1
while (r <= l)
r = m + 1, l = m - 1
class Solution {
public int search(int[] nums, int target) {
int l = 0, r = nums.length - 1;
while (l <= r) {
int m = l + (r - l >> 1);
if (nums[m] < target) {
l = m + 1;
} else if (nums[m] > target) {
r = m - 1;
} else {
return m;
}
}
return -1;
}
}