https://leetcode.cn/problems/search-insert-position/
简单二分,这里可以判断return,相当于剪枝
这里的写法最后更新后的l或r一定可以使得nums[l] 或者nums[r]>=target
所以退出循环最后的l或r就是第一个大于等于target的下标
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int l=0,r=nums.size()-1;
if(target>nums[r])return r+1;
while(l<r)
{
int mid=l+r>>1;
if(nums[mid]>=target)r=mid;
else l=mid+1;
}
return l;
}
};
标签:return,target,nums,int,mid,35,插入,leetcode From: https://www.cnblogs.com/lxl-233/p/17334489.html