当前位置:网站首页>leetcode704. Binary search (find an element, simple, different writing)
leetcode704. Binary search (find an element, simple, different writing)
2022-07-06 07:03:00 【Heavy garbage】
The main points of : Always maintain the same interval as the initial position .
1: Left open right open
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: Left and right closed
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: Left open right closed
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 deviation r On one side , Otherwise, it will fall into a dead cycle
if (nums[mid] == target) return mid;
else if (nums[mid] > target) r = mid - 1;
else l = mid;
}
return -1;
}
};
4: Left closed right away
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;
}
};
边栏推荐
- Blue Bridge Cup zero Foundation National Championship - day 20
- 接口自动化测试框架:Pytest+Allure+Excel
- UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details
- Do you really know the use of idea?
- 【Hot100】739. Daily temperature
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
- Windows Server 2016 standard installing Oracle
- 微信公众号无限回调授权系统源码 全网首发
- kubernetes集群搭建Zabbix监控平台
- 漏了监控:Zabbix对Eureka instance状态监控
猜你喜欢
Hydra common commands
Basic commands of MySQL
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5
机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
SAP SD发货流程中托盘的管理
idea控制台彩色日志
云上有AI,让地球科学研究更省力
开源的网易云音乐API项目都是怎么实现的?
随机推荐
前缀和数组系列
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
What does UDP attack mean? UDP attack prevention measures
BUU的MISC(不定时更新)
基于PyTorch和Fast RCNN快速实现目标识别
The difference between get and post request types
作者已死?AI正用藝術征服人類
leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
数据仓库建设思维导图
将ue4程序嵌入qt界面显示
树莓派串口登录与SSH登录方法
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
Bio model realizes multi person chat
UniPro甘特图“初体验”:关注细节背后的多场景探索
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
ROS learning_ Basics
Apache DolphinScheduler源码分析(超详细)
Every API has its foundation when a building rises from the ground
TS基础篇
Establishment and operation of cloud platform open source project environment