当前位置:网站首页>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;
}
};
边栏推荐
- 19. Actual memory management of segment page combination
- Do you really know the use of idea?
- Apache DolphinScheduler源码分析(超详细)
- Leetcode - 152 product maximum subarray
- 云上有AI,让地球科学研究更省力
- Day 239/300 注册密码长度为8~14个字母数字以及标点符号至少包含2种校验
- Number of query fields
- hydra常用命令
- Introduction to ros2 installation and basic knowledge
- Map of mL: Based on the adult census income two classification prediction data set (whether the predicted annual income exceeds 50K), use the map value to realize the interpretable case of xgboost mod
猜你喜欢

Do you really know the use of idea?

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

On the first day of clock in, click to open a surprise, and the switch statement is explained in detail

Development of entity developer database application

Basic commands of MySQL

Apache DolphinScheduler源码分析(超详细)

Proteus -- Serial Communication parity flag mode

Bitcoinwin (BCW): the lending platform Celsius conceals losses of 35000 eth or insolvency

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

What is the difference between int (1) and int (10)? Senior developers can't tell!
随机推荐
ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用Shap值对XGBoost模型实现可解释性案例之详细攻略
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
Day 239/300 注册密码长度为8~14个字母数字以及标点符号至少包含2种校验
Latex文字加颜色的三种办法
AI on the cloud makes earth science research easier
Pymongo gets a list of data
《从0到1:CTFer成长之路》书籍配套题目(周更)
Short video, more and more boring?
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
成功解决AttributeError: Can only use .cat accessor with a ‘category‘ dtype
pymongo获取一列数据
[hot100] 739. Température quotidienne
Reflex WMS中阶系列3:显示已发货可换组
[English] Grammar remodeling: the core framework of English Learning -- English rabbit learning notes (1)
Fedora/REHL 安装 semanage
Delete external table source data
Day 246/300 ssh连接提示“REMOTE HOST IDENTIFICATION HAS CHANGED! ”
成功解决TypeError: data type ‘category‘ not understood
这个高颜值的开源第三方网易云音乐播放器你值得拥有
Visitor tweets about how you can layout the metauniverse