当前位置:网站首页>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;
}
};
边栏推荐
- [unity] how to export FBX in untiy
- Call, apply, bind rewrite, easy to understand with comments
- The registration password of day 239/300 is 8~14 alphanumeric and punctuation, and at least 2 checks are included
- 【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
- 雲上有AI,讓地球科學研究更省力
- C语言_双创建、前插,尾插,遍历,删除
- P5706 [deep foundation 2. Example 8] redistributing fat house water -- February 13, 2022
- SAP SD发货流程中托盘的管理
- Classification des verbes reconstruits grammaticalement - - English Rabbit Learning notes (2)
- 开源的网易云音乐API项目都是怎么实现的?
猜你喜欢
Lesson 7 tensorflow realizes convolutional neural network
What is the difference between int (1) and int (10)? Senior developers can't tell!
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
Proteus -- Serial Communication parity flag mode
Market segmentation of supermarket customers based on purchase behavior data (RFM model)
My creation anniversary
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
Entity Developer数据库应用程序的开发
A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
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
随机推荐
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
Automated test environment configuration
成功解决TypeError: data type ‘category‘ not understood
UniPro甘特图“初体验”:关注细节背后的多场景探索
Simple use of MySQL database: add, delete, modify and query
Misc of BUU (update from time to time)
Depth residual network
[English] Grammar remodeling: the core framework of English Learning -- English rabbit learning notes (1)
RichView TRVStyle 模板样式的设置与使用
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
librosa音频处理教程
Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
Day 246/300 SSH connection prompt "remote host identification has changed!"
Leetcode daily question (1870. minimum speed to arrive on time)
A brief introduction of reverseme in misc in the world of attack and defense
ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用Shap值对XGBoost模型实现可解释性案例之详细攻略
SSO process analysis
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
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
MySQL high frequency interview 20 questions, necessary (important)