class Solution {
public int searchInsert(int[] nums, int target) {
int l = 0, r = nums.length - 1;
while (l < r) {
int mid = l + ((r - l) >> 1);
if (nums[mid] >= target) r = mid;
else l = mid + 1;
}
// 二分到右边界时,还要比nums[nums.length - 1] 大,说明这个target 应该插入到nums.length 位置上
if (nums[r] < target && r == nums.length - 1) {
return r + 1;
}
return l;
}
}
标签:target,nums,int,mid,35,插入,length,搜索
From: https://www.cnblogs.com/eiffelzero/p/16880188.html