-
二分查找也属于结果集缩小案例:
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
相关推荐
刘恒1234567893 分钟前
Windows 电脑文件夹手动分类指南爱学习的小可爱卢4 分钟前
JavaSE基础-Java异常处理全解析:从ClassNotFound到IndexOutOfBounds小王不爱笑13210 分钟前
LangChain4j 项目实战--1:硅谷小智(医疗智能客服)笔记XiaoLeisj10 分钟前
Android Activity 页面导航基础:Manifest 声明、Intent 显式/隐式跳转与数据传递忍者必须死13 分钟前
JDK1.7的HashMap的环形链表鹿角片ljp16 分钟前
短信登录:基于 Session 实现(黑马点评实战)NEXT0617 分钟前
数组转树与树转数组北风toto17 分钟前
JDK8(JAVA)供应商说明清水白石00818 分钟前
观察者模式全解析:用 Python 构建优雅的事件系统,让组件彻底解耦仟濹18 分钟前
【算法打卡day10(2026-02-24 周二)动态规划DP基础理论】