-
二分查找也属于结果集缩小案例:
package com.nami.algorithm.study.day06;
/**
-
beyond u self and trust u self.
-
@Author: lbc
-
@Date: 2023-09-05 10:09
-
@email: 594599620@qq.com
-
@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
相关推荐
小bo波1 天前
从"任意文件复制"深挖Java I/O:字符流与字节流的本质抉择JieE2122 天前
LeetCode 101. 对称二叉树|JS 递归 + 迭代双解法,彻底搞懂镜像判断nanxun8862 天前
记一次诡异的 Docker 容器"串包"故障排查用户1563068103512 天前
Day01 | Java 基础(Java SE)行者全栈架构师2 天前
Maven dependency:tree 的 8 个高级用法行者全栈架构师2 天前
IDEA 中 Maven 项目的 15 个红色报错快速解决方法令人头秃的代码0_02 天前
mac(m5)平台编译openjdkJieE2123 天前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题Jack203 天前
HarmonyOS开发中错误处理策略:网络异常统一处理小小杨树3 天前
读懂色彩:拍照调色不再难