当前位置:网站首页>Leetcode35. search the insertion position (simple, find the insertion position, different writing methods)
Leetcode35. search the insertion position (simple, find the insertion position, different writing methods)
2022-07-06 07:03:00 【Heavy garbage】
Two points : Library function
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
return lower_bound(nums.begin(), nums.end(), target) - nums.begin();
}
};
Follow leetcode leetcode704. Two points search ( Find an element , Simple , Different ways of writing )
The intervals are different , there [l, r] yes [0, n] instead of [0, n - 1]
https://blog.csdn.net/zhangjiaji111/article/details/125601772
Right border n Is an out of bounds array , If you open it on the right , Will visit nums[n], Therefore, the right side can only be closed with the right : Left and right closed perhaps Left open right closed
1: Left open right closed
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int l = -1, r = nums.size();
while (l + 1 < r) {
int mid = l + (r - l) / 2;
if (nums[mid] >= target) r = mid;
else l = mid;
}
return r;
}
};
2: Left and right closed
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int n = nums.size();
int l = 0, r = n;
while (l < r) {
// When l==r Should exit the cycle when , So there was no ==
int mid = l + (r - l) / 2;
if (nums[mid] >= target) r = mid;
else l = mid + 1;
}
return l; //return r Also on the
}
};
What to pay attention to :
边栏推荐
- 【Hot100】739. 每日温度
- 同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
- Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
- leetcode59. 螺旋矩阵 II(中等)
- AI on the cloud makes earth science research easier
- GET 和 POST 请求类型的区别
- 19. Actual memory management of segment page combination
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
- 作者已死?AI正用艺术征服人类
- ROS学习_基础
猜你喜欢
NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
Development of entity developer database application
基于PyTorch和Fast RCNN快速实现目标识别
Reflex WMS中阶系列3:显示已发货可换组
呆错图床系统源码图片CDN加速与破解防盗链功能
ROS学习_基础
接口自动化测试框架:Pytest+Allure+Excel
L'auteur est mort? Ai utilise l'art pour conquérir l'humanité
ROS learning_ Basics
Do you really know the use of idea?
随机推荐
[advanced software testing step 1] basic knowledge of automated testing
云上有AI,让地球科学研究更省力
《从0到1:CTFer成长之路》书籍配套题目(周更)
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
Zhongqing reading news
Fedora/REHL 安装 semanage
Huawei equipment configuration ospf-bgp linkage
简单描述 MySQL 中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)
Top test sharing: if you want to change careers, you must consider these issues clearly!
Librosa audio processing tutorial
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
librosa音频处理教程
漏了监控:Zabbix对Eureka instance状态监控
软件测试外包到底要不要去?三年真实外包感受告诉你
这个高颜值的开源第三方网易云音乐播放器你值得拥有
作者已死?AI正用艺术征服人类
TS Basics
Apache dolphin scheduler source code analysis (super detailed)
Raspberry pie 3B update VIM