当前位置:网站首页>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也对
}
};
注意的地方:
边栏推荐
- 【软件测试进阶第1步】自动化测试基础知识
- hydra常用命令
- 基于PyTorch和Fast RCNN快速实现目标识别
- 成功解决TypeError: data type ‘category‘ not understood
- 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
- What is the biggest problem that fresh e-commerce is difficult to do now
- Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
- ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用Shap值对XGBoost模型实现可解释性案例之详细攻略
- 软件测试外包到底要不要去?三年真实外包感受告诉你
- LeetCode Algorithm 2181. 合并零之间的节点
猜你喜欢

On the first day of clock in, click to open a surprise, and the switch statement is explained in detail

(practice C language every day) reverse linked list II

详解SQL中Groupings Sets 语句的功能和底层实现逻辑

How to find a medical software testing institution? First flight software evaluation is an expert

Huawei equipment configuration ospf-bgp linkage

Market segmentation of supermarket customers based on purchase behavior data (RFM model)

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

ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用Shap值对XGBoost模型实现可解释性案例之详细攻略

My seven years with NLP

Do you really know the use of idea?
随机推荐
UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
Latex文字加颜色的三种办法
软件测试外包到底要不要去?三年真实外包感受告诉你
LeetCode每日一题(1870. Minimum Speed to Arrive on Time)
ROS2安装及基础知识介绍
Pallet management in SAP SD delivery process
Misc of BUU (update from time to time)
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
(practice C language every day) reverse linked list II
[Yu Yue education] flower cultivation reference materials of Weifang Vocational College
开源的网易云音乐API项目都是怎么实现的?
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
Three methods of adding color to latex text
mysql的基础命令
A brief introduction of reverseme in misc in the world of attack and defense
Leetcode daily question (1870. minimum speed to arrive on time)
PCL实现选框裁剪点云
3. Business and load balancing of high architecture
Suspended else