当前位置:网站首页>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 :
边栏推荐
- 19.段页结合的实际内存管理
- BUU的MISC(不定时更新)
- 【刷题】怎么样才能正确的迎接面试?
- C language_ Double create, pre insert, post insert, traverse, delete
- Huawei equipment configuration ospf-bgp linkage
- [hot100] 739. Température quotidienne
- SAP SD发货流程中托盘的管理
- Day 248/300 thoughts on how graduates find jobs
- ROS学习_基础
- leetcode704. 二分查找(查找某个元素,简单,不同写法)
猜你喜欢

微信公众号无限回调授权系统源码 全网首发
![[brush questions] how can we correctly meet the interview?](/img/89/a5b874ba4db97fbb3d330af59c387a.png)
[brush questions] how can we correctly meet the interview?
![[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5](/img/c3/7a147151b7338cf38ffbea24e8bafd.jpg)
[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5

攻防世界 MISC中reverseMe简述

Pallet management in SAP SD delivery process

Blue Bridge Cup zero Foundation National Championship - day 20

Prefix and array series

UWA pipeline version 2.2.1 update instructions

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

Depth residual network
随机推荐
ROS学习_基础
A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
“无聊猿” BAYC 的内忧与外患
Thought map of data warehouse construction
Misc of BUU (update from time to time)
How to find a medical software testing institution? First flight software evaluation is an expert
Reflex WMS medium level series 3: display shipped replaceable groups
L'auteur est mort? Ai utilise l'art pour conquérir l'humanité
SEO学习的最好方式:搜索引擎
Proteus -- Serial Communication parity flag mode
18. Multi level page table and fast table
What is the difference between int (1) and int (10)? Senior developers can't tell!
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
Wechat official account infinite callback authorization system source code, launched in the whole network
Hydra common commands
万丈高楼平地起,每个API皆根基
Zhongqing reading news
When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
Top test sharing: if you want to change careers, you must consider these issues clearly!
数据仓库建设思维导图