当前位置:网站首页>二分查找法
二分查找法
2022-07-13 17:58:00 【柠ning】
二分查找法
在这里我就是说说我自己的想法。这里困难的,我想就是容易分不清边界,仅仅去选择记忆那个模板(在过了很久以后就会忘记)
二分查找法:
有两个前提条件:1.在查找的内容范围上一定是有顺序的。
2.一次只能查找一个数。
假设一个有序的数组{1,2,3,4,5},查找一个targeta=4的位置,运用二分查找可以很快的找到。
在二分查找法中,对于范围的界定十分严谨,每种界定的定义方法也是不一样。
二分查找方法是不停的进行去迭代,所以划定一个正确的方法是非常重要的。(也就是左右区间开闭的不同)。
- 左闭右闭
- 左闭右开
用力扣上的一道题来进行解释:
题目:
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的
target,如果目标值存在返回下标,否则返回 -1。
示例一:
输入: nums = [-1,0,3,5,9,12], target = 9 输出: 4 解释: 9 出现在 nums 中并且下标为 4
示例二:
输入: nums = [-1,0,3,5,9,12], target = 2 输出: -1 解释: 2 不存在 nums 中因此返回 -1
- 首先选择数组中间的数字和需要查找的目标值比。
- 如果相等最好,就可以直接返回答案了。
- 如果不相等(while循环)
- 如果中间的数字大于目标值,则中间数字向右的所有数字都大于目标值,全部排除。
- 如果中间的数字小于目标值,则中间数字向左的所有数字都小于目标值,全部排除。
二分法就是用这种方法来进行快速查找的。
把上面那个代码题解决一下
左闭右闭
class Solution {
public int search(int[] nums, int target) {
// 避免当 target 小于nums[0] nums[nums.length - 1]时多次循环运算
if (target < nums[0] || target > nums[nums.length - 1]) {
return -1;
}
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + ((right - left) >> 1);
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
left = mid + 1;
else if (nums[mid] > target)
right = mid - 1;
}
return -1;
}
}
左闭右开
class Solution {
public int search(int[] nums, int target) {
int left = 0, right = nums.length;
while (left < right) {
int mid = left + ((right - left) >> 1);
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
left = mid + 1;
else if (nums[mid] > target)
right = mid;
}
return -1;
}
}
其实大概是相同的。
边栏推荐
- LeetCode 刷题第十三天 + DL第三天
- Promise --- synchronize? Asynchronous?
- Introduction to vscode plug-in installation
- [introduction to go language] 14 go language goroutine and channel details
- js知识点
- 01kNN_ Regression
- [hbuilderx development uniapp] automatic code formatting plug-in installation and configuration
- How to select embedded single chip microcomputer?
- LeetCode 第十六天
- Pagoda panel creates multiple port deployment projects under the same server (lightweight application server one click deployment website, blog, gltlab full version)
猜你喜欢
随机推荐
RT_ Thread producer and consumer issues
LeetCode 刷题 第九天
用户模块开发
Vscode automatically adds comments
Promise --- synchronize? Asynchronous?
SSM整合(借鉴版)
线性代数知识回顾:矩阵的秩,矩阵的范数,矩阵的条件数,矩阵的特征值和特征向量
Introduction to vscode plug-in installation
Leetcode-2 addition of two numbers (head insertion, tail insertion of linked list, addition and subtraction of two linked lists with different lengths)
Pagoda panel creates multiple port deployment projects under the same server (lightweight application server one click deployment website, blog, gltlab full version)
[introduction to go language] 10 go language map details
[introduction to go language] 12 go language structure (struct) detailed explanation
Rtthread creation of thread
leetcode-2 两数相加(链表的头插法、尾插法、两个不同长度链表相加、减操作的处理方法)
vim用法
LeetCode 刷题 第四天
Implementation of array flattening
leetcode-3 无重复字符的最长字串(滑动窗口,unordered_set, st.find(string[i]))
ArkUI路由跳转概览
Swagger quick start (interface documentation)


![[Go语言入门] 06 Go语言循环语句](/img/c1/097bbd37199d2755caccf64c4ae162.png)



![[go language introduction] 13 go language interface details](/img/38/7576257ecc706251e23b8a5ec2d98e.png)


