-
二分查找也属于结果集缩小案例:
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
相关推荐
Navigator_Z29 分钟前
LeetCode //C - 1318. Minimum Flips to Make a OR b Equal to c电科_银尘1 小时前
【大模型算法与工程 39】PPO:策略比率、裁剪目标与训练稳定性shehuiyuelaiyuehao2 小时前
算法68,字符串相乘Huangjin007_3 小时前
【C++ 算法竞赛篇】STL 常用知识点速通指南驭渊的小故事4 小时前
LeetCode 33:搜索旋转排序数组——从二分查找到旋转数组的区间判定htzyl2066 小时前
金额计算:为什么前端不该用浮点数存钱电科_银尘6 小时前
【大模型算法与工程 32】全量微调与参数高效微调:如何选择落羽的落羽7 小时前
【C++】现代容器: tuple、optional、variant、any、string_view电科_银尘7 小时前
【大模型算法与工程 38】RLHF与奖励模型:偏好如何变成训练信号会编程的吕洞宾8 小时前
Spring AI 2.0 工具调用实战:让 AI 自动调用你的 Java 方法