当前位置:网站首页>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 :
边栏推荐
猜你喜欢

Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL

配置树莓派接入网络

首发织梦百度推送插件全自动收录优化seo收录模块

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

Bitcoinwin (BCW): the lending platform Celsius conceals losses of 35000 eth or insolvency

Depth residual network

同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励

kubernetes集群搭建Zabbix监控平台

Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-

leetcode841. 钥匙和房间(中等)
随机推荐
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
雲上有AI,讓地球科學研究更省力
What is the difference between int (1) and int (10)? Senior developers can't tell!
UniPro甘特图“初体验”:关注细节背后的多场景探索
万丈高楼平地起,每个API皆根基
Arduino tutorial - Simon games
Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
Do you really know the use of idea?
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
Every API has its foundation when a building rises from the ground
Compile, connect -- notes-2
Development of entity developer database application
WPF之MVVM
这个高颜值的开源第三方网易云音乐播放器你值得拥有
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)
Interface automation test framework: pytest+allure+excel
接口自动化测试框架:Pytest+Allure+Excel
The difference between get and post request types
SEO学习的最好方式:搜索引擎
ROS learning_ Basics