有序数组中插入位置
给定一个有序数组和一个目标值,找到目标值在有序数组中的插入位置。比如:
[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0
int searchInsert(vector<int>& nums, int target)
{
int start = 0;
int last = nums.size();
while (start < last)
{
int mid = (start + last) / 2;
if (nums[mid] >= target)
{
last = mid;
}
else
{
start = mid + 1;
}
}
return start;
}声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: 无法从指针(Node *)类型转换为const指针(const Node *&)
- 下一篇:没有了
