当前位置:网站首页>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;
}
};
边栏推荐
- Day 246/300 SSH connection prompt "remote host identification has changed!"
- Every API has its foundation when a building rises from the ground
- After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
- 接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
- leetcode1020. 飞地的数量(中等)
- 【Hot100】739. 每日温度
- A brief introduction of reverseme in misc in the world of attack and defense
- Apache DolphinScheduler源码分析(超详细)
- Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
- What is the biggest problem that fresh e-commerce is difficult to do now
猜你喜欢
ROS learning_ Basics
1189. Maximum number of "balloons"
L'Ia dans les nuages rend la recherche géoscientifique plus facile
Raspberry pie 3B update VIM
When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
C language_ Double create, pre insert, post insert, traverse, delete
idea控制台彩色日志
Development of entity developer database application
【软件测试进阶第1步】自动化测试基础知识
雲上有AI,讓地球科學研究更省力
随机推荐
配置树莓派接入网络
Bitcoinwin (BCW): the lending platform Celsius conceals losses of 35000 eth or insolvency
When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
数据仓库建设思维导图
微信公众号无限回调授权系统源码 全网首发
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
中青看点阅读新闻
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
Depth residual network
Database basics exercise part 2
简单描述 MySQL 中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)
Applied stochastic process 01: basic concepts of stochastic process
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
【Hot100】739. 每日溫度
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
leetcode841. 钥匙和房间(中等)
Raspberry pie serial port login and SSH login methods
“无聊猿” BAYC 的内忧与外患
Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
万丈高楼平地起,每个API皆根基