当前位置:网站首页>35. 搜索插入位置
35. 搜索插入位置
2022-07-26 10:42:00 【Forest_1010】

使用二分法
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int low=0,high=nums.size()-1,middle;
while(low<=high)//当left==right,区间[left, right]依然有效
{
middle=(low+high)/2;
if(target<nums[middle])
{
high=middle-1;
}
else if(target>nums[middle])
{
low=middle+1;
}
else
{
return middle;// 数组中找到目标值的情况,直接返回下标
}
}
return high+1;
}
};
时间复杂度:O(logn)
空间复杂度:O(1)
边栏推荐
- Issue 7: how do you choose between curling up and lying flat
- Sql Server 之SQL语句对基本表及其中的数据的创建和修改
- 智能合约dapp系统开发流程技术
- Flutter 防止科学计数并去除尾数无效0
- .net operation redis sorted set ordered set
- [leetcode daily question 2021/8/30]528. Choose randomly by weight [medium]
- 【小程序】onReachBottom 事件为什么不能触发 ?(一秒搞定)
- Phase 4: one of College Students' vocational skills preparation in advance
- 鹏哥C语言20210811程序结构作业
- RT-Thread 学习笔记(八)---开启基于SPI Flash的elmfat文件系统(下)
猜你喜欢
![[leetcode daily question 2021/4/29]403. Frogs cross the river](/img/fb/612777c77df5a611506e72f4f4c3c8.png)
[leetcode daily question 2021/4/29]403. Frogs cross the river

Zongzi battle - guess who can win
![[notes on machine learning] [building a cyclic neural network and its application] deeplearning ai course5 1st week programming(keras)](/img/02/f85da2a2f2524fb034b17ed8d06692.png)
[notes on machine learning] [building a cyclic neural network and its application] deeplearning ai course5 1st week programming(keras)

工厂模式详解
![[leetcode daily question 2021/2/13]448. Find all the missing numbers in the array](/img/9b/624416fa6a408bf64ca5438273176b.png)
[leetcode daily question 2021/2/13]448. Find all the missing numbers in the array

解决:无法加载文件 C:\Users\user\AppData\Roaming\npm\npx.ps1,因为在此系统上禁止运行脚本 。

Issue 7: how do you choose between curling up and lying flat

2021-08-12函数递归_和鹏哥学习C语言

【机器学习小记】【人脸识别】deeplearning.ai course4 4th week programming

RT-Thread 学习笔记(六)--- 开启基于SPI Flash的elmfat文件系统(上)
随机推荐
剑指Offer(七):斐波那契数列
MFC多线程的简单使用
字典与int矩阵
The problem of formatting IAR sprintf floating point to 0.0 in UCOS assembly
回到顶部的几种方案(js)
Flutter TextField设置高度并且自动换行,圆角边框去除下划线
12 don't forget every component when copying an object
Simple use of json-c Library -- converting JSON files to struct
多目标优化系列1---NSGA2的非支配排序函数的讲解
px2rem-loader将px转化为rem,适配移动端vant-UI等框架
Flutter CachedNetworkImage圆角
flutter dart生成N个区间范围内不重复随机数List
Add touch screen driver for stemwin 5.22 on Shenzhou IV development board
在altium designer中禁用USBJATG
剑指Offer(九):变态跳台阶
Halcon模板匹配之Shape
剑指Offer(四十三):左旋转字符串
Issue 8: cloud native -- how should college students learn in the workplace
C language callback function
STM32 Alibaba cloud mqtt esp8266 at command