当前位置:网站首页>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;
}
};
边栏推荐
- C语言_双创建、前插,尾插,遍历,删除
- LeetCode - 152 乘积最大子数组
- Simple query cost estimation
- 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
- 机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
- When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
- Visitor tweets about how you can layout the metauniverse
- 18. Multi level page table and fast table
- 【软件测试进阶第1步】自动化测试基础知识
猜你喜欢
Pallet management in SAP SD delivery process
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
前缀和数组系列
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
Windows Server 2016 standard installing Oracle
Fast target recognition based on pytorch and fast RCNN
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
My creation anniversary
[ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)
随机推荐
LeetCode每日一题(971. Flip Binary Tree To Match Preorder Traversal)
这个高颜值的开源第三方网易云音乐播放器你值得拥有
Automated test environment configuration
SSO process analysis
Database basics exercise part 2
Proteus -- Serial Communication parity flag mode
WPF之MVVM
Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting
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
Number of query fields
Misc of BUU (update from time to time)
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
Reflex WMS中阶系列3:显示已发货可换组
Day 248/300 thoughts on how graduates find jobs
PCL realizes frame selection and clipping point cloud
Introduction to ros2 installation and basic knowledge
前缀和数组系列
C语言_双创建、前插,尾插,遍历,删除
The difference between get and post request types
基于PyTorch和Fast RCNN快速实现目标识别