当前位置:网站首页>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)
边栏推荐
- 2021-08-12函数递归_和鹏哥学习C语言
- 在altium designer中禁用USBJATG
- display-inline+calc实现左中右布局,中间自适应
- 7-25 0-1背包 (50分)
- 第5期:大学生入职必备技能之二
- Successfully transplanted stemwin v5.22 on Shenzhou IV development board
- Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images
- 从蚂蚁的觅食过程看团队研发(转载)
- kali 查看ip地址
- [leetcode daily question 2021/2/18] [detailed explanation] minimum number of turns of 995. K continuous bits
猜你喜欢
![[leetcode daily question 2021/2/14]765. Lovers hold hands](/img/be/8639a05c733638bf0b3fdeb11abccf.png)
[leetcode daily question 2021/2/14]765. Lovers hold hands

RT-Thread 学习笔记(六)--- 开启基于SPI Flash的elmfat文件系统(上)

Codepoint 58880 not found in font, aborting. flutter build apk时报错

Issue 5: the second essential skill for College Students

Add touch screen driver for stemwin 5.22 on Shenzhou IV development board

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

第7期:内卷和躺平,你怎么选

Write to esp8266 burning brush firmware

Oracle cannot start tnslistener service cannot start

RT-Thread 学习笔记(一)---配置RT-Thread开发环境
随机推荐
The problem of formatting IAR sprintf floating point to 0.0 in UCOS assembly
使用float实现左中右布局,中间内容自适应
C语言鹏哥20210812C语言函数
toolstrip 去边框
剑指Offer(二十一):栈的压入、弹出序列
[leetcode daily question 2021/2/13]448. Find all the missing numbers in the array
The problem of large fluctuation of hx711 data
使用flex实现左中右布局,中间自适应
鹏哥C语言第六节课
Sql Server之查询总结
SuperMap IClient for Leaflet 加载高斯克吕格投影三度分带CGCS2000大地坐标系WMTS服务
RT-Thread 学习笔记(六)--- 开启基于SPI Flash的elmfat文件系统(上)
GIS方法类期刊和论文的综述(Introduction)怎么写?
Flutter集成极光推送
RT-Thread 学习笔记(三)---用SCons 构建编译环境
使用定位实现左中右布局,中间内容自适应
剑指Offer(八):跳台阶
Common classes (understand)
反射机制简述
点击el-dropdown-item/@click.native