ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ // 二分查找算法 public class Algorithm0005 { public static void main(String[] args) { int[] nums = new int[]{3, 5, 7, 8, 10, 22, 88, 500, 999, 1000}; int index = find(nums, nums.length / 2, 1000); System.out.print(index); } private static int find(int[] nums, int base, int target) { if (null == nums || 0 == nums.length) { throw new RuntimeException("nums is empty"); } if (nums[base] < target) { int index = (nums.length + base) / 2; return find(nums, index, target); } else if (nums[base] > target) { return find(nums, base / 2, target); } else { return base; } } } ~~~