数据结构 递归解决二分查找 数据结构(八)

  1. 二分查找也属于结果集缩小案例:

    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);
      }
      }

相关推荐
52Hz1188 小时前
力扣24.两两交换链表中的节点、25.K个一组反转链表
算法·leetcode·链表
老鼠只爱大米8 小时前
LeetCode经典算法面试题 #160:相交链表(双指针法、长度差法等多种方法详细解析)
算法·leetcode·链表·双指针·相交链表·长度差法
一个龙的传说8 小时前
xshell下载
java
ValhallaCoder9 小时前
Day53-图论
数据结构·python·算法·图论
老鼠只爱大米9 小时前
LeetCode经典算法面试题 #84:柱状图中最大的矩形(单调栈、分治法等四种方法详细解析)
算法·leetcode·动态规划·单调栈·分治法·柱状图最大矩形
C雨后彩虹9 小时前
羊、狼、农夫过河
java·数据结构·算法·华为·面试
java资料站9 小时前
SpringAI+DeepSeek大模型应用开发实战
java
Elastic 中国社区官方博客9 小时前
使用瑞士风格哈希表实现更快的 ES|QL 统计
大数据·数据结构·sql·elasticsearch·搜索引擎·全文检索·散列表
进击的CJR9 小时前
redis cluster 部署
java·redis·mybatis
重生之后端学习9 小时前
19. 删除链表的倒数第 N 个结点
java·数据结构·算法·leetcode·职场和发展