当前位置:网站首页>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 :
边栏推荐
- C语言_双创建、前插,尾插,遍历,删除
- A brief introduction of reverseme in misc in the world of attack and defense
- kubernetes集群搭建Zabbix监控平台
- Development of entity developer database application
- 19. Actual memory management of segment page combination
- 数据仓库建设思维导图
- Simple use of MySQL database: add, delete, modify and query
- 云上有AI,让地球科学研究更省力
- Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
- Chapter 7 - thread pool of shared model
猜你喜欢

升级版手机检测微信工具小程序源码-支持多种流量主模式

Blue Bridge Cup zero Foundation National Championship - day 20

BUU的MISC(不定时更新)

Setting and using richview trvstyle template style

AI on the cloud makes earth science research easier

WPF之MVVM

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数据库应用程序的开发

18. Multi level page table and fast table

UWA pipeline version 2.2.1 update instructions
随机推荐
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
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
【Hot100】739. Daily temperature
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
Leetcode - 152 product maximum subarray
ROS learning_ Basics
Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
【每日一题】729. 我的日程安排表 I
Huawei equipment configuration ospf-bgp linkage
18. Multi level page table and fast table
Interface automation test framework: pytest+allure+excel
Development of entity developer database application
雲上有AI,讓地球科學研究更省力
【刷题】怎么样才能正确的迎接面试?
AI on the cloud makes earth science research easier
Visitor tweets about how you can layout the metauniverse
Fedora/rehl installation semanage
树莓派3B更新vim
Reflex WMS中阶系列3:显示已发货可换组
【软件测试进阶第1步】自动化测试基础知识