-
二分查找也属于结果集缩小案例:
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
相关推荐
52Hz1188 小时前
力扣24.两两交换链表中的节点、25.K个一组反转链表老鼠只爱大米8 小时前
LeetCode经典算法面试题 #160:相交链表(双指针法、长度差法等多种方法详细解析)一个龙的传说8 小时前
xshell下载ValhallaCoder9 小时前
Day53-图论老鼠只爱大米9 小时前
LeetCode经典算法面试题 #84:柱状图中最大的矩形(单调栈、分治法等四种方法详细解析)C雨后彩虹9 小时前
羊、狼、农夫过河java资料站9 小时前
SpringAI+DeepSeek大模型应用开发实战Elastic 中国社区官方博客9 小时前
使用瑞士风格哈希表实现更快的 ES|QL 统计进击的CJR9 小时前
redis cluster 部署重生之后端学习9 小时前
19. 删除链表的倒数第 N 个结点