当前位置:网站首页>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;
}
};
边栏推荐
- Delete external table source data
- Arduino tutorial - Simon games
- Leetcode daily question (971. flip binary tree to match preorder traversal)
- Fedora/REHL 安装 semanage
- [daily question] 729 My schedule I
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
- 19. Actual memory management of segment page combination
- What is the difference between int (1) and int (10)? Senior developers can't tell!
- UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details
- Do you really know the use of idea?
猜你喜欢

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

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

同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励

Leetcode daily question (971. flip binary tree to match preorder traversal)

女生学软件测试难不难 入门门槛低,学起来还是比较简单的

SQL Server Manager studio (SSMS) installation tutorial

Classification des verbes reconstruits grammaticalement - - English Rabbit Learning notes (2)

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

Windows Server 2016 standard installing Oracle

How to reconstruct the class explosion caused by m*n strategies?
随机推荐
MySQL high frequency interview 20 questions, necessary (important)
Fast target recognition based on pytorch and fast RCNN
UDP攻击是什么意思?UDP攻击防范措施
Suspended else
Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
Depth residual network
Day 248/300 关于毕业生如何找工作的思考
从autojs到冰狐智能辅助的心里历程
hydra常用命令
Day 239/300 注册密码长度为8~14个字母数字以及标点符号至少包含2种校验
【每日一题】729. 我的日程安排表 I
Successfully solved typeerror: data type 'category' not understood
Introduction to ros2 installation and basic knowledge
Leetcode - 152 product maximum subarray
因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
Every API has its foundation when a building rises from the ground
Prefix and array series
【Hot100】739. Daily temperature
[Yu Yue education] Dunhuang Literature and art reference materials of Zhejiang Normal University