当前位置:网站首页>[21-Day Learning Challenge] A small summary of sequential search and binary search
[21-Day Learning Challenge] A small summary of sequential search and binary search
2022-08-02 00:18:00 【Xiao Lu wants to brush the force and deduct the question】
活动地址:CSDN21天学习挑战赛
文章目录
顺序查找
What is a sequential search
Sequential search is often used in our life
For example, we want to find a card in a pile of playing cards,
How do we find it
The easiest way is to find them one by one
This is the principle of sequential search
Sequential search is to traverse the array from the beginning to the end,Find the desired number

代码
for(int i=0;i<arr.length;i++){
if(arr[i]==target){
return i;
}
}
时间复杂度
Because the worst case is to traverse the array,
因此时间复杂度为O(n)
*二分查找
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1.
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/binary-search
著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处.
什么是二分查找
以升序数列为例,比较一个元素与数列中的中间位置的元素的大小,如果比中间位置的元素大,则继续在后半部分的数列中进行二分查找;如果比中间位置的元素小,则在数列的前半部分进行比较;如果相等,则找到了元素的位置.每次比较的数列长度都会是之前数列的一半,直到找到相等元素的位置或者最终没有找到要找的元素.
简单来说,Just look for the big ones to the right,If you see a small one, look left

nums[m]<target,So look on the right
nums[m]==target,ans=5
代码
class Solution {
public int search(int[] nums, int target) {
int n=nums.length;
int l=0;
int r=n-1;
while(l<=r){
int m=l+(r-l)/2;
if(nums[m]<target){
l=m+1;
}else if(nums[m]>target){
r=m-1;
}else{
return m;
}
}
return -1;
}
}
进阶:在一个有序数组中,找>=某个数最左侧的位置

一直二分到死
代码
public static int nearestIndex(int[] arr, int value) {
int L = 0;
int R = arr.length - 1;
int index = -1; // 记录最左的对号
while (L <= R) {
// 至少一个数的时候
int mid = L + ((R - L) >> 1);
if (arr[mid] >= value) {
index = mid;
R = mid - 1;
} else {
L = mid + 1;
}
}
return index;
}
同理,在一个有序数组中,找<=某个数最右侧的位置,It's the same solution
public static int nearestIndex(int[] arr, int value) {
int L = 0;
int R = arr.length - 1;
int index = -1; // 记录最右的对号
while (L <= R) {
int mid = L + ((R - L) >> 1);
if (arr[mid] <= value) {
index = mid;
L = mid + 1;
} else {
R = mid - 1;
}
}
return index;
}
进阶2:局部最小值问题
在一个无序数组中, The value may be positive, 负, 或者零, Any two adjacent numbers in an array must not be equal.
Define a local minimum:
1.长度为1,arr[0]就是局部最小;
2.数组的开头,如果arr[0] < arr[1] ,arr[0]is defined as a local minimum.
3.数组的结尾,如果arr[N-1] < arr[N-2] ,arr[N-1]is defined as a local minimum.
any intermediate positioni, 即数组下标1~N-2之间, 必须满足arr[i-1] < arr[i] <arr[i+1] ,is called finding a local minimum.
Find any local minimum and return it.
解题思路
先单独看0位置和n-1位置,If neither side is a local minimum,Then if you connect each number of the array on the coordinate axis,There must be a local minimum position,从中间分开,If the middle position is not a local minimum,It doesn't matter which half it is,There are also local minima,Something like this builds something like exclusivity,two points,

代码
public static int getLessIndex(int[] arr) {
if (arr == null || arr.length == 0) {
return -1;
}
if (arr.length == 1 || arr[0] < arr[1]) {
return 0;
}
if (arr[arr.length - 1] < arr[arr.length - 2]) {
return arr.length - 1;
}
int left = 1;
int right = arr.length - 2;
int mid = 0;
while (left < right) {
mid = (left + right) / 2;
if (arr[mid] > arr[mid - 1]) {
right = mid - 1;
} else if (arr[mid] > arr[mid + 1]) {
left = mid + 1;
} else {
return mid;
}
}
return left;
}
二分总结
1)数据状况特殊
2)The problem itself is special
Dichotomy does not necessarily require order
Depends what your problem is,Depends on what your data status is
As long as you can build something exclusive, There must be half on the left and right sides,The other half may not
If you are only looking for one,Just cut in half, As long as you can build something like exclusivity, You get two points,不一定要求数组有序
边栏推荐
- security cross-domain configuration
- 含外部储能的电力系统暂态稳定分布式控制
- Detailed explanation of Zadig's self-testing and tuning environment technical solution for developers
- 基于数据驱动的变电站巡检机器人自抗扰控制
- QML包管理
- security CSRF Vulnerability Protection
- 面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值
- 在不完全恢复、控制文件被创建或还原后,必须使用 RESETLOGS 打开数据库,解释 RESETLOGS.
- 微软电脑管家V2.1公测版正式发布
- 协作乐高 All In One:DAO工具大全
猜你喜欢

IP Core: FIFO

SphereEx苗立尧:云原生架构下的Database Mesh研发实践

如何发现新的潜力项目?工具推荐

Don't know about SynchronousQueue?So ArrayBlockingQueue and LinkedBlockingQueue don't and don't know?

Short video seo search optimization main content

【三子棋】C语言实现简易三子棋

Double queue implementation stack?Dual stack implementation queue?

面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值

不就是个TCC分布式事务,有那么难吗?

控制电机的几种控制电路原理图
随机推荐
Arduino Basic Syntax
信息物理系统状态估计与传感器攻击检测
Unknown CMake command “add_action_files“
[Three sons] C language implements simple three sons
一篇永久摆脱Mysql时区错误问题,idea数据库可视化插件配置
如何重装Win11?一键重装Win11方法
Short video SEO optimization tutorial Self-media SEO optimization skills and methods
【加密周报】经济衰退在加息气氛中蔓延 美联储“放手一搏”?盘点上周加密市场发生的重大事件
easy-excel 解决百万数据导入导出,性能很强
Keepalived 高可用的三种路由方案
Is TCP reliable?Why?
2022/08/01 学习笔记 (day21) 泛型和枚举
Axure教程-新手入门基础(小白强烈推荐!!!)
Double queue implementation stack?Dual stack implementation queue?
双队列实现栈?双栈实现队列?
图解LeetCode——1161. 最大层内元素和(难度:中等)
基于相关性变量筛选偏最小二乘回归的多维相关时间序列建模方法
els block boundary deformation processing
els block deformation judgment.
Detailed explanation of Zadig's self-testing and tuning environment technical solution for developers