当前位置:网站首页>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;
}
};
边栏推荐
- [daily question] 729 My schedule I
- Apache dolphin scheduler source code analysis (super detailed)
- Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
- What does UDP attack mean? UDP attack prevention measures
- Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
- Latex文字加颜色的三种办法
- BUU的MISC(不定时更新)
- 作者已死?AI正用艺术征服人类
- CDN acceleration and cracking anti-theft chain function
- Reflex WMS medium level series 3: display shipped replaceable groups
猜你喜欢

After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.

攻防世界 MISC中reverseMe简述

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

Short video, more and more boring?

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

What is the difference between int (1) and int (10)? Senior developers can't tell!

这个高颜值的开源第三方网易云音乐播放器你值得拥有

UWA Pipeline 2.2.1 版本更新说明

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

hydra常用命令
随机推荐
数据仓库建设思维导图
Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
微信脑力比拼答题小程序_支持流量主带最新题库文件
树莓派串口登录与SSH登录方法
Leetcode - 152 product maximum subarray
机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
Thought map of data warehouse construction
编译,连接 -- 笔记 -2
Short video, more and more boring?
Zhongqing reading news
Fast target recognition based on pytorch and fast RCNN
【Hot100】739. 每日溫度
Prefix and array series
Top test sharing: if you want to change careers, you must consider these issues clearly!
18.多级页表与快表
Database basics exercise part 2
BUU的MISC(不定时更新)
WPF之MVVM
【Hot100】739. Daily temperature
How to find a medical software testing institution? First flight software evaluation is an expert