当前位置:网站首页>leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
2022-07-06 06:55:00 【重you小垃】


二分:库函数
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
return lower_bound(nums.begin(), nums.end(), target) - nums.begin();
}
};
跟 leetcode leetcode704. 二分查找(查找某个元素,简单,不同写法)
区间不同,这里的[l, r] 是 [0, n] 而不是[0, n - 1]
https://blog.csdn.net/zhangjiaji111/article/details/125601772
右边界n是越界数组的,如果用右开,会访问到nums[n],因此右边只能用右闭:左闭右闭 或者 左开右闭
1:左开右闭
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:左闭右闭
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int n = nums.size();
int l = 0, r = n;
while (l < r) {
//当l==r时应该退出循环,所以没有==
int mid = l + (r - l) / 2;
if (nums[mid] >= target) r = mid;
else l = mid + 1;
}
return l; //return r也对
}
};
注意的地方:
边栏推荐
- LeetCode Algorithm 2181. 合并零之间的节点
- Database basics exercise part 2
- Windows Server 2016 standard installing Oracle
- GET 和 POST 请求类型的区别
- Pymongo gets a list of data
- SSO流程分析
- Leetcode daily question (1870. minimum speed to arrive on time)
- LeetCode每日一题(1997. First Day Where You Have Been in All the Rooms)
- Fedora/REHL 安装 semanage
- Day 245/300 JS forEach 多层嵌套后数据无法更新到对象中
猜你喜欢

Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks

云上有AI,让地球科学研究更省力

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

开源的网易云音乐API项目都是怎么实现的?

Pallet management in SAP SD delivery process

Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债

LeetCode每日一题(971. Flip Binary Tree To Match Preorder Traversal)

Introduction and underlying analysis of regular expressions

AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm

How to reconstruct the class explosion caused by m*n strategies?
随机推荐
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
LeetCode - 152 乘积最大子数组
BUU的MISC(不定时更新)
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
Delete external table source data
librosa音频处理教程
云上有AI,让地球科学研究更省力
Lesson 7 tensorflow realizes convolutional neural network
ROS2安装及基础知识介绍
医疗软件检测机构怎么找,一航软件测评是专家
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
【Hot100】739. 每日溫度
Database basics exercise part 2
Proteus -- Serial Communication parity flag mode
机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
开源的网易云音乐API项目都是怎么实现的?
Huawei equipment configuration ospf-bgp linkage
Call, apply, bind rewrite, easy to understand with comments
At the age of 26, I changed my career from finance to software testing. After four years of precipitation, I have been a 25K Test Development Engineer
Day 246/300 SSH connection prompt "remote host identification has changed!"