力扣704/35/34:二分查找

server/2024/9/23 6:39:06/

考虑到线性查找法的时间复杂度较高(O(n)), 我们可以选择使用二分查找算法.

二分查找算法只适用于有序数组(线性查找不需要满足该前提), 其时间复杂度为O(logn), 我们可以选择两种方式来完成二分查找算法.

要求 : 给定一个有序整形数组, 在该数组中, 找到目标值target, 如果找到, 则返回其在数组中的索引, 否则返回-1.

(1)方法1 : 指针左闭右闭[i, j]----> 推导出循环条件 : i <= j

java">public static int BinarySearch_1(int[] arr, int target) {int i = 0;int j = arr.length - 1;//设置指针和初值while (i <= j) {int mid = i + (j - i) / 2;if (arr[mid] == target) {return mid;} else if (arr[mid] < target) {i = mid + 1;} else {j = mid - 1;}}return -1;}

方式2 : 指针左闭右开[i, j) -----> 推导出循环条件 : i < j

java">public static int BinarySearch_2(int[] arr, int target) {int i = 0;int j = arr.length;while (i < j) {int mid = i + (j - i) / 2;if (arr[mid] > target) {j = mid;} else if (arr[mid] < target) {i = mid + 1;} else {return mid;}}return -1;}

test : 

java">@Testpublic void test1() {int[] arr = {1, 4, 6, 9, 11, 15, 57};int target = 9;int index1 = BinarySearch_1(arr, target);System.out.println("索引处是" + index1);int index2 = BinarySearch_2(arr, target);System.out.println("索引处是" + index2);}控制台 : 
索引处是3
索引处是3

而对于有重复性元素的数组来说, 查找值恰好为该重复的元素值, 上面两种方式返回的索引即为第一次被查找到的索引位置,.

test : 

java">@Testpublic void test1() {int[] arr = {9, 9, 9, 9, 9, 9};int target = 9;int index1 = BinarySearch_1(arr, target);System.out.println("索引处是" + index1);int index2 = BinarySearch_2(arr, target);System.out.println("索引处是" + index2);}控制台 : 
索引处是2
索引处是3

如果我们要求返回的索引是重复元素最左边的位置的索引, 即如上, 应返回索引0.我们应该对算法做出怎样的变化呢?

java">public static int BinarySearch_3(int[] arr, int target) {int i = 0;int j = arr.length - 1;//设置候选索引int cadicate = -1;int mid = i + (j - i) / 2;while (i <= j) {mid = i + (j - i) / 2;if (arr[mid] > target) {j = mid - 1;} else if (arr[mid] < target) {i = mid + 1;} else {//如果arr[mid] == target, 并不退出循环, 而是更新候选值, 继续向左排查cadicate = mid;j = mid - 1;}}return cadicate == -1 ? -1 : cadicate;}

test : 

java">@Testpublic void test1() {int[] arr = {1, 4, 7, 7, 7, 7,  8, 10};int target = 7;int index3 = BinarySearch_3(arr, target);System.out.println("索引处是" + index3);}控制台 : 
索引处是2

上述代码完成了返回了最左边的元素的索引, 如果我们想要得到最右边的元素的索引, 应该怎么办呢?非常简单, 只需要在else语句处略微改动.

java">else {//如果arr[mid] == target, 并不退出循环, 而是更新候选值, 继续向右排查cadicate = mid;i = mid + 1;
}

test : 

java">@Testpublic void test1() {int[] arr = {1, 4, 7, 7, 7, 7, 7, 8, 10};int target = 7;int index4 = BinarySearch_4(arr, target);System.out.println("索引处是" + index4);}控制台 : 
索引处是6

http://www.ppmy.cn/server/15074.html

相关文章

网络协议安全:OSI七层模型分层及作用,数据封装与解封过程,数据传输过程。

「作者简介」&#xff1a;2022年北京冬奥会中国代表队&#xff0c;CSDN Top100&#xff0c;学习更多干货&#xff0c;请关注专栏《网络安全自学教程》 这一章节我们需要知道OSI分哪七层&#xff0c;每层的作用&#xff0c;知道数据在七层模型中是怎样传输的&#xff0c;封包和解…

java算法day5

哈希表基础哈希表写题基础字符串类有效的字母异位词ArrayList用法两个数组的交集两数之和 哈希表基础 哈希函数&#xff1a; 哈希表使用哈希函数将键转换为数组的索引。理想情况下&#xff0c;哈希函数应该将键均匀分布在数组中&#xff0c;以减少冲突&#xff08;两个键映射到…

2018年华三杯山东省赛决赛实验

2018年华三杯山东省赛决赛实验 拓扑图 配置需求 请考生根据以下配置需求在 HCL中的设备上进行相关配置。 网络设备虚拟化 数据中心交换机需要实现虚拟化。支持的虚拟化技术 IRF,所配置的参数要求如下: 链形堆叠,IRF Domain 值为 10; IRF1的 member ID 为 1,IRF2的 member …

wordpress建网站主题案例推荐

wordpress企业网站主题案例 https://www.mymoban.com/wordpress/ wordpress公司官网主题案例 https://www.wowsoho.com/jianzhan wordpress外贸主题案例 https://www.wpniu.com/moban

Redis网络模型

目录 1. Redis命令执行部分为什么使用单线程 2. 单线程下&#xff0c;Redis如何实现高性能的&#xff1f; reactor模型 3. 单线程部分 函数initServer 函数aeMain 服务端可读&#xff0c;执行acceptTcpHandler 客户端可读&#xff0c;执行readQueryFromClient 1. 读取…

开源事件通知库libevent及网络连接管理模块bufferevent详解

目录 1、libevent介绍 1.1、什么是libevent&#xff1f; 1.2、libevent特点 1.3、网络连接管理模块bufferevent 2、bufferevent有什么用&#xff1f; 3、bufferevent的整体设计与实现细节 3.1、整体概况 3.2、evbuffer与bufferevent 3.3、defer callback 4、bufferev…

三、搭建 VLC,实战点播功能

目录 1、VLC 播放器 2、VLC media player 3、VLC 打开网络串流 4、VLC 系统支持

2024HW --->蓝队面试题

这段时间在写横向移动&#xff0c;搞得鸽了很久&#xff08;内网真的很玄学&#xff09; 还没写完。。。 但是这不是准备HW了吗。小编也来整理一下自己收集到的题目吧&#xff01;&#xff01;&#xff01; &#xff08;仅为个人见解&#xff0c;不代表最终答案&#xff09;&…