当前位置:网站首页>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 :
边栏推荐
- Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案
- 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)
- 攻防世界 MISC中reverseMe简述
- 数据仓库建设思维导图
- 顶测分享:想转行,这些问题一定要考虑清楚!
- SSO process analysis
- UDP攻击是什么意思?UDP攻击防范措施
- 【软件测试进阶第1步】自动化测试基础知识
- leetcode704. 二分查找(查找某个元素,简单,不同写法)
- Raspberry pie 3B update VIM
猜你喜欢

作者已死?AI正用藝術征服人類

ROS learning_ Basics

After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.

树莓派3B更新vim

Huawei equipment configuration ospf-bgp linkage

Apache dolphin scheduler source code analysis (super detailed)

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

19. Actual memory management of segment page combination

【软件测试进阶第1步】自动化测试基础知识

指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
随机推荐
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
Three methods of adding color to latex text
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
A brief introduction of reverseme in misc in the world of attack and defense
The difference between get and post request types
Apache dolphin scheduler source code analysis (super detailed)
leetcode1020. 飞地的数量(中等)
呆错图床系统源码图片CDN加速与破解防盗链功能
作者已死?AI正用藝術征服人類
A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
C语言_双创建、前插,尾插,遍历,删除
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
[brush questions] how can we correctly meet the interview?
Pallet management in SAP SD delivery process
Misc of BUU (update from time to time)
开源的网易云音乐API项目都是怎么实现的?
[hot100] 739. Température quotidienne
L'Ia dans les nuages rend la recherche géoscientifique plus facile
Introduction to ros2 installation and basic knowledge
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm