当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

1189. Maximum number of "balloons"

C语言_双创建、前插,尾插,遍历,删除

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

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

How to reconstruct the class explosion caused by m*n strategies?

作者已死?AI正用艺术征服人类

Hydra common commands

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

Kubernetes cluster builds ZABBIX monitoring platform

kubernetes集群搭建Zabbix监控平台
随机推荐
C language_ Double create, pre insert, post insert, traverse, delete
【每日一题】729. 我的日程安排表 I
漏了监控:Zabbix对Eureka instance状态监控
【Hot100】739. 每日温度
Leetcode daily question (1870. minimum speed to arrive on time)
Arduino tutorial - Simon games
L'Ia dans les nuages rend la recherche géoscientifique plus facile
[brush questions] how can we correctly meet the interview?
win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
攻防世界 MISC中reverseMe简述
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
树莓派3B更新vim
同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
Refer to how customer push e-commerce does content operation
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
Raspberry pie 3B update VIM
NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
leetcode1020. 飞地的数量(中等)
巴比特 | 元宇宙每日必读:中国互联网企业涌入元宇宙的群像:“只有各种求生欲,没有前瞻创新的雄心”...