当前位置:网站首页>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;
}
};
边栏推荐
- Wechat official account infinite callback authorization system source code, launched in the whole network
- Huawei equipment configuration ospf-bgp linkage
- 数据仓库建设思维导图
- Visitor tweets about how you can layout the metauniverse
- librosa音频处理教程
- Briefly describe the differences between indexes, primary keys, unique indexes, and joint indexes in mysql, and how they affect the performance of the database (in terms of reading and writing)
- Database basics exercise part 2
- 开源的网易云音乐API项目都是怎么实现的?
- SEO学习的最好方式:搜索引擎
- 升级版手机检测微信工具小程序源码-支持多种流量主模式
猜你喜欢

Chapter 7 - thread pool of shared model

Raspberry pie 3B update VIM

RichView TRVStyle 模板样式的设置与使用

Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent

leetcode1020. 飞地的数量(中等)

How to find a medical software testing institution? First flight software evaluation is an expert

顶测分享:想转行,这些问题一定要考虑清楚!

NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections

接口自动化测试框架:Pytest+Allure+Excel

18. Multi level page table and fast table
随机推荐
Depth residual network
Missing monitoring: ZABBIX monitors the status of Eureka instance
UWA Pipeline 2.2.1 版本更新说明
Simple use of MySQL database: add, delete, modify and query
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
Applied stochastic process 01: basic concepts of stochastic process
When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details
“无聊猿” BAYC 的内忧与外患
LeetCode Algorithm 2181. 合并零之间的节点
Top test sharing: if you want to change careers, you must consider these issues clearly!
微信公众号无限回调授权系统源码 全网首发
树莓派3B更新vim
Prefix and array series
BUU的MISC(不定时更新)
简单描述 MySQL 中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)
Zhongqing reading news
The psychological process from autojs to ice fox intelligent assistance
win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问