当前位置:网站首页>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也对
}
};
注意的地方:
边栏推荐
- [unity] how to export FBX in untiy
- [hot100] 739. Température quotidienne
- Classification des verbes reconstruits grammaticalement - - English Rabbit Learning notes (2)
- [ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)
- LeetCode - 152 乘积最大子数组
- Huawei equipment configuration ospf-bgp linkage
- 详解SQL中Groupings Sets 语句的功能和底层实现逻辑
- Successfully solved typeerror: data type 'category' not understood
- Day 248/300 关于毕业生如何找工作的思考
- Erreur de type résolue avec succès: type de données « catégorie» non sous - jacente
猜你喜欢
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
Simple use of MySQL database: add, delete, modify and query
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
Suspended else
L'Ia dans les nuages rend la recherche géoscientifique plus facile
My seven years with NLP
Every API has its foundation when a building rises from the ground
Pallet management in SAP SD delivery process
Bitcoinwin (BCW): the lending platform Celsius conceals losses of 35000 eth or insolvency
开源的网易云音乐API项目都是怎么实现的?
随机推荐
[unity] how to export FBX in untiy
Apache dolphin scheduler source code analysis (super detailed)
(practice C language every day) reverse linked list II
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
Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting
【Hot100】739. Daily temperature
Classification des verbes reconstruits grammaticalement - - English Rabbit Learning notes (2)
Suspended else
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
Leetcode - 152 product maximum subarray
Leetcode daily question (1870. minimum speed to arrive on time)
mysql的基础命令
成功解决TypeError: data type ‘category‘ not understood
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
My creation anniversary
BUU的MISC(不定时更新)
ROS学习_基础
Cif10 actual combat (resnet18)
简单描述 MySQL 中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)
SQL Server Manager studio (SSMS) installation tutorial