当前位置:网站首页>leetcode:704二分查找
leetcode:704二分查找
2022-07-28 11:37:00 【Memorises1999】
今天开始慢慢刷leetcode了,跟着网上的大牛刷题路线一步一步走着,具体的路线为:数组-> 链表-> 哈希表->字符串->栈与队列->树->回溯->贪心->动态规划->图论->高级数据结构
第一步为:数组
相关的理论基础
1、数组是存放在连续内存空间上的相同类型数据的集合。
2、数组下标都是从0开始的,数组内存空间的地址是连续的
我们在删除或者增添元素的时候,就难免要移动其他元素的地址
3、c++中二维数组在地址空间上是连续的,而JAVA则不是
题目描述:给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1
解题思路:
1、二分查找的前提条件:1有序数组,2数组中无重复元素
2、二分查找法涉及区间范围、边界条件。要想清楚对区间的定义,根据区间的定义来对边界条件进行处理
3、采用两种区间方法来进行解题:左闭右闭,左闭右开
方法一:左闭右闭,也就是[left, right]
要点:
- while (left <= right) 要使用 <= ,因为left == right是有意义的,所以使用 <=
- if (nums[middle] > target) :right 要赋值为 middle - 1,因为当前这个nums[middle]一定不是target,那么接下来要查找的左区间结束下标位置就是 middle - 1
具体代码实现
// 版本一
class Solution {
public:
int search(vector<int>& nums, int target) {
int left = 0;
int right = nums.size() - 1; // 定义target在左闭右闭的区间里,[left, right]
while (left <= right) { // 当left==right,区间[left, right]依然有效,所以用 <=
int middle = left + ((right - left) / 2);// 防止溢出 等同于(left + right)/2
if (nums[middle] > target) {
right = middle - 1; // target 在左区间,所以[left, middle - 1]
} else if (nums[middle] < target) {
left = middle + 1; // target 在右区间,所以[middle + 1, right]
} else { // nums[middle] == target
return middle; // 数组中找到目标值,直接返回下标
}
}
// 未找到目标值
return -1;
}
};
方法二:左闭右开也就是[left, right)
要点:
- while (left < right),这里使用 < ,因为left == right在区间[left, right)是没有意义的
- if (nums[middle] > target) right 更新为 middle,因为当前nums[middle]不等于target,去左区间继续寻找,而寻找区间是左闭右开区间,所以right更新为middle,即:下一个查询区间不会去比较nums[middle]
具体代码实现
// 版本二
class Solution {
public:
int search(vector<int>& nums, int target) {
int left = 0;
int right = nums.size(); // 定义target在左闭右开的区间里,即:[left, right)
while (left < right) { // 因为left == right的时候,在[left, right)是无效的空间,所以使用 <
int middle = left + ((right - left) >> 1);
if (nums[middle] > target) {
right = middle; // target 在左区间,在[left, middle)中
} else if (nums[middle] < target) {
left = middle + 1; // target 在右区间,在[middle + 1, right)中
} else { // nums[middle] == target
return middle; // 数组中找到目标值,直接返回下标
}
}
// 未找到目标值
return -1;
}
};总结:
1、关于二分查找最重要的就是要明确自己定义的区间是什么,根据所定义的区间进行边界条件的处理
边栏推荐
- 行业落地呈现新进展 | 2022 开放原子全球开源峰会 OpenAtom OpenHarmony 分论坛圆满召开
- PHP时间戳相减转化为天小时分秒
- Localization, low latency, green and low carbon: Alibaba cloud officially launched Fuzhou data center
- 微创电生理通过注册:年营收1.9亿 微创批量生产上市企业
- PHP date time application: add or subtract the number of days of a specific date
- Laravel $object->updated_ At returns the carbon object. How to return the normal time format
- Fusion cloud native, enabling new mileage | 2022 open atom global open source summit cloud native sub forum successfully held
- 奥浦迈生物通过注册:半年营收1.47亿 国寿成达与达晨是股东
- Arduino Pro Mini atmega328p connect and light the first LED (at the same time, record the problem of burning failure stk500_recv)
- laravel表单数据验证
猜你喜欢

Localization, low latency, green and low carbon: Alibaba cloud officially launched Fuzhou data center

Why do enterprises need the ability of enterprise knowledge management?

arduino pro mini ATMEGA328P 连线和点亮第一盏LED(同时记录烧录失败的问题stk500_recv)

Uniapp wechat applet realizes the function of connecting low-power Bluetooth printing

Is it difficult for cloud native machine learning to land? Lingqueyun helps enterprises quickly apply mlops

用C语言开发NES游戏(CC65)10、游戏循环

Zadig v1.13.0 believes in the power of openness, and workflow connects all values

Yan Ji lost Beijing again, and more than half of the stores in the country were closed

数字经济时代的开源数据库创新 | 2022 开放原子全球开源峰会数据库分论坛圆满召开
![[dark horse morning post] LETV 400 employees have no 996 and no internal papers; Witness history! 1 euro =1 US dollar; Stop immediately when these two interfaces appear on wechat; The crackdown on cou](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[dark horse morning post] LETV 400 employees have no 996 and no internal papers; Witness history! 1 euro =1 US dollar; Stop immediately when these two interfaces appear on wechat; The crackdown on cou
随机推荐
遭受痛苦和创伤后的四种本真姿态 齐泽克
How does musk lay off staff?
开源社区三十年 | 2022 开放原子全球开源峰会开源社区三十年专题活动圆满召开
【Try to Hack】udf提权
用C语言开发NES游戏(CC65)09、滚动
1331. 数组序号转换 : 简单模拟题
用C语言开发NES游戏(CC65)10、游戏循环
Kuzaobao: summary of Web3 encryption industry news on July 13
Great! Jd.com developed the highly available website construction technology PDF recommended by the first brother. Prepare the water and chew it slowly
After abolishing Tencent cloud: meiyabaike won the bid of 98.3 million
用C语言开发NES游戏(CC65)07、控制器
云原生机器学习落地难?灵雀云助力企业快速应用 MLOps
PHP日期时间运用:添加或减去特定日期的天数
软件架构师必需要了解的 saas 架构设计?
Sub database and sub table may not be suitable for your system. Let's talk about how to choose sub database and sub table and newsql
Laravel form data validation
论治理与创新 | 2022 开放原子全球开源峰会 OpenAnolis 分论坛圆满召开
How can a novice quickly complete the establishment of a website? Come to the free "fitting room" experience
易观分析:以用户为中心提升手机银行用户体验,助力用户价值增长
SQL注入 Less24(二次注入)