当前位置:网站首页>leetcode704. 二分查找(查找某个元素,简单,不同写法)
leetcode704. 二分查找(查找某个元素,简单,不同写法)
2022-07-06 06:55:00 【重you小垃】


要点:始终维护的区间和初始时的位置是一样的即可。
1:左开右开
class Solution {
public:
int search(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) return mid;
else if (nums[mid] > target) r = mid;
else l = mid;
}
return -1;
}
};
2:左闭右闭
class Solution {
public:
int search(vector<int>& nums, int target) {
int l = 0, r = nums.size() - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (nums[mid] == target) return mid;
else if (nums[mid] > target) r = mid - 1;
else l = mid + 1;
}
return -1;
}
};
3:左开右闭
class Solution {
public:
int search(vector<int>& nums, int target) {
int l = -1, r = nums.size() - 1;
while (l < r) {
int mid = l + (r - l) / 2 + 1;//mid偏向r一边,否则会陷入死循环
if (nums[mid] == target) return mid;
else if (nums[mid] > target) r = mid - 1;
else l = mid;
}
return -1;
}
};
4:左闭右开
class Solution {
public:
int search(vector<int>& nums, int target) {
int l = 0, r = nums.size();
while (l < r) {
int mid = l + (r - l) / 2;
if (nums[mid] == target) return mid;
else if (nums[mid] > target) r = mid;
else l = mid + 1;
}
return -1;
}
};
边栏推荐
- My creation anniversary
- Simple use of MySQL database: add, delete, modify and query
- Fedora/rehl installation semanage
- Database basics exercise part 2
- Suspended else
- Do you really know the use of idea?
- 前缀和数组系列
- Delete external table source data
- The difference between get and post request types
- Day 245/300 JS forEach 多层嵌套后数据无法更新到对象中
猜你喜欢

Development of entity developer database application

Introduction to ros2 installation and basic knowledge

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

前缀和数组系列

After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious

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

A brief introduction of reverseme in misc in the world of attack and defense

Proteus -- Serial Communication parity flag mode

Reflex WMS medium level series 3: display shipped replaceable groups

Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
随机推荐
Pymongo gets a list of data
Latex文字加颜色的三种办法
L'Ia dans les nuages rend la recherche géoscientifique plus facile
一文读懂简单查询代价估算
kubernetes集群搭建Zabbix监控平台
Introduction to ros2 installation and basic knowledge
基于购买行为数据对超市顾客进行市场细分(RFM模型)
【每日一题】729. 我的日程安排表 I
LeetCode每日一题(1870. Minimum Speed to Arrive on Time)
Suspended else
Cif10 actual combat (resnet18)
同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用Shap值对XGBoost模型实现可解释性案例之详细攻略
[Yu Yue education] flower cultivation reference materials of Weifang Vocational College
[advanced software testing step 1] basic knowledge of automated testing
将ue4程序嵌入qt界面显示
自动化测试环境配置
UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details
Misc of BUU (update from time to time)
19.段页结合的实际内存管理