当前位置:网站首页>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 :
边栏推荐
- Setting and using richview trvstyle template style
- 基于PyTorch和Fast RCNN快速实现目标识别
- pymongo获取一列数据
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
- AI on the cloud makes earth science research easier
- Basic commands of MySQL
- The difference between get and post request types
- SAP SD发货流程中托盘的管理
- Chapter 7 - thread pool of shared model
- Automated test environment configuration
猜你喜欢
Depth residual network
Wechat official account infinite callback authorization system source code, launched in the whole network
Database basics exercise part 2
Reflex WMS中阶系列3:显示已发货可换组
《从0到1:CTFer成长之路》书籍配套题目(周更)
顶测分享:想转行,这些问题一定要考虑清楚!
NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
OpenGL ES 学习初识(1)
Raspberry pie serial port login and SSH login methods
“无聊猿” BAYC 的内忧与外患
随机推荐
Map of mL: Based on the adult census income two classification prediction data set (whether the predicted annual income exceeds 50K), use the map value to realize the interpretable case of xgboost mod
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
C语言_双创建、前插,尾插,遍历,删除
Entity Developer数据库应用程序的开发
Latex文字加颜色的三种办法
librosa音频处理教程
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
巴比特 | 元宇宙每日必读:中国互联网企业涌入元宇宙的群像:“只有各种求生欲,没有前瞻创新的雄心”...
pymongo获取一列数据
How to reconstruct the class explosion caused by m*n strategies?
Raspberry pie 3B update VIM
Do you really know the use of idea?
云上有AI,让地球科学研究更省力
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
编译,连接 -- 笔记 -2
OpenGL ES 学习初识(1)
18. Multi level page table and fast table
Introduction to ros2 installation and basic knowledge
LeetCode 78:子集