当前位置:网站首页>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 :
边栏推荐
猜你喜欢
Wechat official account infinite callback authorization system source code, launched in the whole network
BUU的MISC(不定时更新)
Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案
[daily question] 729 My schedule I
树莓派串口登录与SSH登录方法
ROS2安装及基础知识介绍
Misc of BUU (update from time to time)
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
随机推荐
Bitcoinwin (BCW): the lending platform Celsius conceals losses of 35000 eth or insolvency
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
Entity Developer数据库应用程序的开发
[hot100] 739. Température quotidienne
C language_ Double create, pre insert, post insert, traverse, delete
When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
Compile, connect -- notes-2
微信公众号无限回调授权系统源码 全网首发
Reflex WMS中阶系列3:显示已发货可换组
机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
Proteus -- Serial Communication parity flag mode
Simple use of MySQL database: add, delete, modify and query
[some special grammars about C]
19.段页结合的实际内存管理
Database basics exercise part 2
18.多级页表与快表
SEO学习的最好方式:搜索引擎
Reflex WMS medium level series 3: display shipped replaceable groups
Fedora/rehl installation semanage
攻防世界 MISC中reverseMe简述