当前位置:网站首页>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;
}
};
边栏推荐
- librosa音频处理教程
- Reflex WMS medium level series 3: display shipped replaceable groups
- Setting and using richview trvstyle template style
- Leetcode - 152 product maximum subarray
- 因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
- leetcode59. 螺旋矩阵 II(中等)
- Missing monitoring: ZABBIX monitors the status of Eureka instance
- The difference between get and post request types
- MPLS experiment
- Zhongqing reading news
猜你喜欢

Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple

Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation

Apache DolphinScheduler源码分析(超详细)

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

leetcode6109. 知道秘密的人数(中等,周赛)

When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward

作者已死?AI正用藝術征服人類

Do you really know the use of idea?
![[advanced software testing step 1] basic knowledge of automated testing](/img/3d/f83f792e24efc39f00c0dc33936ce8.png)
[advanced software testing step 1] basic knowledge of automated testing

Database basics exercise part 2
随机推荐
【软件测试进阶第1步】自动化测试基础知识
Prefix and array series
【Hot100】739. 每日溫度
Latex文字加颜色的三种办法
Do you really know the use of idea?
Apache dolphin scheduler source code analysis (super detailed)
编译,连接 -- 笔记 -2
Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details
Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
Embed UE4 program into QT interface display
leetcode59. 螺旋矩阵 II(中等)
Misc of BUU (update from time to time)
Applied stochastic process 01: basic concepts of stochastic process
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
Fedora/rehl installation semanage
19.段页结合的实际内存管理
机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
Zhongqing reading news
ROS learning_ Basics