当前位置:网站首页>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也对
}
};
注意的地方:
边栏推荐
- Do you really know the use of idea?
- 成功解决AttributeError: Can only use .cat accessor with a ‘category‘ dtype
- C语言_双创建、前插,尾插,遍历,删除
- Database basics exercise part 2
- Machine learning plant leaf recognition
- 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
- Introduction to ros2 installation and basic knowledge
- UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details
- Day 246/300 SSH connection prompt "remote host identification has changed!"
- Misc of BUU (update from time to time)
猜你喜欢
(practice C language every day) reverse linked list II
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
Market segmentation of supermarket customers based on purchase behavior data (RFM model)
Reflex WMS medium level series 3: display shipped replaceable groups
Windows Server 2016 standard installing Oracle
Monotonic stack
Visitor tweets about how you can layout the metauniverse
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
Development of entity developer database application
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
随机推荐
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
漏了监控:Zabbix对Eureka instance状态监控
Depth residual network
What is the difference between int (1) and int (10)? Senior developers can't tell!
BUU的MISC(不定时更新)
将ue4程序嵌入qt界面显示
Latex文字加颜色的三种办法
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
Fast target recognition based on pytorch and fast RCNN
万丈高楼平地起,每个API皆根基
【Hot100】739. 每日温度
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
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
女生学软件测试难不难 入门门槛低,学起来还是比较简单的
Simple use of JWT
基于PyTorch和Fast RCNN快速实现目标识别
软件测试外包到底要不要去?三年真实外包感受告诉你
Embed UE4 program into QT interface display
Day 239/300 注册密码长度为8~14个字母数字以及标点符号至少包含2种校验
机器学习植物叶片识别