-
二分查找也属于结果集缩小案例:
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
相关推荐
Tisfy2 分钟前
LeetCode 955.删列造序 II:模拟(O(mn)) + 提前退出雨中飘荡的记忆5 分钟前
享元模式深度解析:看Java如何优雅节省内存悟空码字6 分钟前
SpringBoot接口防抖大作战,拒绝“手抖”重复提交!im_AMBER7 分钟前
Leetcode 82 每个字符最多出现两次的最长子字符串 | 删掉一个元素以后全为 1 的最长子数组编程大师哥11 分钟前
Boost C++java修仙传11 分钟前
力扣hot100:旋转排序数组中找目标值式51613 分钟前
量子力学基础(二)狄拉克符号与复数向量空间网安_秋刀鱼22 分钟前
【java安全】shiro鉴权绕过k***921623 分钟前
【Linux】进程概念(六):地址空间核心机制与遨游于天地23 分钟前
Spring 的10个核心能力,对框架开发的启示