-
二分查找也属于结果集缩小案例:
package com.nami.algorithm.study.day06;
/**
-
beyond u self and trust u self.
-
@Author: lbc
-
@Date: 2023-09-05 10:09
-
@email: [email protected]
-
@Description: keep coding
*/
public class BinarySearch {private static int search(int[] target, int search, int i, int j) {
if (i > j) {
return -1;
}
int temp = (i + j) >>> 1;
if (target[temp] < search) {
return search(target, search, temp + 1, j);
} else if (target[temp] > search) {
return search(target, search, i, temp - 1);
} else {
return temp;
}}
public static int search0(int[] target, int search) {
return search(target, search, 0 , target.length);
}public static void main(String[] args) {
int[] target = new int[]{1, 2, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 19, 23, 24, 25, 26, 27, 28, 57, 234};
int result = search0(target, 234);
System.out.println(result);
}
}
-
数据结构 递归解决二分查找 数据结构(八)
qq_339191142023-09-11 21:22
相关推荐
wang__123002 分钟前
力扣2680题解大G哥18 分钟前
Rust 之 trait 与泛型的奥秘suyukangchen25 分钟前
深入理解 Linux 阻塞IO与Socket数据结构isyangli_blog30 分钟前
(1-1)Java的JDK、JRE、JVM三者间的关系刚入坑的新人编程35 分钟前
数据结构——排序(万字解说)初阶数据结构完GGBondlctrl42 分钟前
【leetcode】《BFS扫荡术:如何用广度优搜索征服岛屿问题》zhuiQiuMX1 小时前
笔试阶段性心得总结星沁城2 小时前
236. 二叉树的最近公共祖先oliveira-time3 小时前
Java 1.8(也称为Java 8)极小狐5 小时前
如何使用极狐GitLab 软件包仓库功能托管 maven?