当前位置:网站首页>leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
2022-07-06 06:55:00 【重you小垃】
二分:库函数
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
return lower_bound(nums.begin(), nums.end(), target) - nums.begin();
}
};
跟 leetcode leetcode704. 二分查找(查找某个元素,简单,不同写法)
区间不同,这里的[l, r] 是 [0, n] 而不是[0, n - 1]
https://blog.csdn.net/zhangjiaji111/article/details/125601772
右边界n是越界数组的,如果用右开,会访问到nums[n],因此右边只能用右闭:左闭右闭 或者 左开右闭
1:左开右闭
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:左闭右闭
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int n = nums.size();
int l = 0, r = n;
while (l < r) {
//当l==r时应该退出循环,所以没有==
int mid = l + (r - l) / 2;
if (nums[mid] >= target) r = mid;
else l = mid + 1;
}
return l; //return r也对
}
};
注意的地方:
边栏推荐
- LeetCode每日一题(971. Flip Binary Tree To Match Preorder Traversal)
- Suspended else
- BIO模型实现多人聊天
- 18.多级页表与快表
- 开源的网易云音乐API项目都是怎么实现的?
- Development of entity developer database application
- C language_ Double create, pre insert, post insert, traverse, delete
- Blue Bridge Cup zero Foundation National Championship - day 20
- SQL Server manager studio(SSMS)安装教程
- LeetCode - 152 乘积最大子数组
猜你喜欢
Office doc add in - Online CS
攻防世界 MISC中reverseMe简述
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
Market segmentation of supermarket customers based on purchase behavior data (RFM model)
Blue Bridge Cup zero Foundation National Championship - day 20
hydra常用命令
19.段页结合的实际内存管理
【刷题】怎么样才能正确的迎接面试?
L'Ia dans les nuages rend la recherche géoscientifique plus facile
Cif10 actual combat (resnet18)
随机推荐
Simple query cost estimation
软件测试外包到底要不要去?三年真实外包感受告诉你
Erreur de type résolue avec succès: type de données « catégorie» non sous - jacente
Lesson 7 tensorflow realizes convolutional neural network
【刷题】怎么样才能正确的迎接面试?
UDP攻击是什么意思?UDP攻击防范措施
成功解决AttributeError: Can only use .cat accessor with a ‘category‘ dtype
LeetCode - 152 乘积最大子数组
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
Apache dolphin scheduler source code analysis (super detailed)
[hot100] 739. Température quotidienne
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
Blue Bridge Cup zero Foundation National Championship - day 20
Database basics exercise part 2
ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用Shap值对XGBoost模型实现可解释性案例之详细攻略
Reflex WMS中阶系列3:显示已发货可换组
At the age of 26, I changed my career from finance to software testing. After four years of precipitation, I have been a 25K Test Development Engineer
19. Actual memory management of segment page combination
Briefly describe the differences between indexes, primary keys, unique indexes, and joint indexes in mysql, and how they affect the performance of the database (in terms of reading and writing)
BUU的MISC(不定时更新)