当前位置:网站首页>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;
}
};
边栏推荐
- Machine learning plant leaf recognition
- The difference between get and post request types
- 因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
- 【Hot100】739. 每日温度
- Do you really know the use of idea?
- Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
- 成功解决TypeError: data type ‘category‘ not understood
- Fast target recognition based on pytorch and fast RCNN
- WPF之MVVM
- Three methods of adding color to latex text
猜你喜欢

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

万丈高楼平地起,每个API皆根基

Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)

Cif10 actual combat (resnet18)

Blue Bridge Cup zero Foundation National Championship - day 20

接口自动化测试实践指导(上):接口自动化需要做哪些准备工作

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

Entity Developer数据库应用程序的开发

漏了监控:Zabbix对Eureka instance状态监控

Introduction and underlying analysis of regular expressions
随机推荐
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
Every API has its foundation when a building rises from the ground
MySQL high frequency interview 20 questions, necessary (important)
【Hot100】739. Daily temperature
漏了监控:Zabbix对Eureka instance状态监控
Misc of BUU (update from time to time)
Machine learning plant leaf recognition
Introduction and underlying analysis of regular expressions
Database basics exercise part 2
顶测分享:想转行,这些问题一定要考虑清楚!
Lesson 7 tensorflow realizes convolutional neural network
Attributeerror successfully resolved: can only use cat accessor with a ‘category‘ dtype
Fedora/rehl installation semanage
Simple query cost estimation
女生学软件测试难不难 入门门槛低,学起来还是比较简单的
Windows Server 2016 standard installing Oracle
雲上有AI,讓地球科學研究更省力
成功解决AttributeError: Can only use .cat accessor with a ‘category‘ dtype
When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
My seven years with NLP