当前位置:网站首页>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;
}
};
边栏推荐
- 成功解决TypeError: data type ‘category‘ not understood
- 开源的网易云音乐API项目都是怎么实现的?
- Day 246/300 SSH connection prompt "remote host identification has changed!"
- 【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
- A brief introduction of reverseme in misc in the world of attack and defense
- A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
- What is the biggest problem that fresh e-commerce is difficult to do now
- Short video, more and more boring?
- My creation anniversary
- Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
猜你喜欢

Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-

Development of entity developer database application

mysql的基础命令

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

Reflex WMS中阶系列3:显示已发货可换组

SAP SD发货流程中托盘的管理

My creation anniversary

Fast target recognition based on pytorch and fast RCNN

基于购买行为数据对超市顾客进行市场细分(RFM模型)

机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
随机推荐
Latex文字加颜色的三种办法
[English] Grammar remodeling: the core framework of English Learning -- English rabbit learning notes (1)
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
LeetCode每日一题(1997. First Day Where You Have Been in All the Rooms)
Windows Server 2016 standard installing Oracle
Embed UE4 program into QT interface display
Simple query cost estimation
Leetcode - 152 product maximum subarray
Redis Foundation
ROS2安装及基础知识介绍
18. Multi level page table and fast table
PCL realizes frame selection and clipping point cloud
Blue Bridge Cup zero Foundation National Championship - day 20
On the first day of clock in, click to open a surprise, and the switch statement is explained in detail
雲上有AI,讓地球科學研究更省力
Huawei equipment configuration ospf-bgp linkage
pymongo获取一列数据
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
SSO流程分析
hydra常用命令