-
二分查找也属于结果集缩小案例:
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
相关推荐
heimeiyingwang25 分钟前
【架构实战】Kubernetes调度器深度剖析:从Pod调度到自定义调度器Jerry1 小时前
LeetCode 108. 将有序数组转换为二叉搜索树蓝斯4972 小时前
Diff算法的简单介绍过期的秋刀鱼!2 小时前
L2正则化,防止过拟合-历史补充纳兰青华2 小时前
回文侦探:三种境界破解最长回文子串hansang_IR2 小时前
【题解】LC:Z 算法(Z Algorithm)范什么特西3 小时前
回答知识总结02橘子汽水1683 小时前
Leetcode 230,98:二叉搜索树中第K小的元素,验证二叉搜索树Zane19943 小时前
CAS 与原子类:Java 如何实现无锁编程TunerT_TQ3 小时前
【智能体安全治理|专栏第9期】从“一堆规则”到“数字宪法”:智能体治理的下一个阶段