模板一
public int search(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while(left <= right) {
int mid = (left + right) / 2;
if(nums[mid] == target) {
// 处理逻辑
} else if(nums[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left;
}
模板二
public int search(int[] nums, int target) {
int left = 0, right = nums.length;
while(left < right) {
int mid = (left + right) / 2;
if(nums[mid] == target) {
// 处理逻辑
} else if(nums[mid] < target) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}