Java描述 LeetCode,704. Binary Search 二分查找,原来二分查找也可以有小优化~

本文探讨了在LeetCode题目中如何用递归和迭代方式实现高效的二分查找算法,并分享了针对边界条件和溢出问题的优化技巧。通过对比代码,展示了细节优化在算法性能上的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大家好,我是河海哥,专注于后端,如果可以的话,想做一名code designer而不是普通的coder,一起见证河海哥的成长,您的评论与赞与关注是我的最大动力,如有错误还请不吝赐教,万分感谢。一起支持原创吧!纯手打有笔误还望谅解。

1-1:题目描述

Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.

You must write an algorithm with O(log n) runtime complexity.

Example 1:

Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4

Example 2:

Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1

Constraints:

1 <= nums.length <= 104
-104 < nums[i], target < 104
All the integers in nums are unique.
nums is sorted in ascending order.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-search
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

1-2:递归解法

不多说,直接上代码。递归作为基础代码,应该都会写的。

public int search(int[] nums, int target) {
    int n = nums.length;
    return recursion(nums, 0, n - 1, target);
}

public int recursion(int[] nums, int first, int last, int target) {
    if (first > last) {
        return -1;
    }
    int mid = (first + last)/2;
    if (nums[mid] > target) {
        return recursion(nums, first, mid - 1, target);
    }
    if (nums[mid] < target) {
        return recursion(nums, mid + 1, last, target);
    }
    return mid;
}

很平淡无奇,但是你再看下面的这一段代码!对比以下,你会发现原来二分查找也会有优化!

public int search(int[] nums, int target) {
    int n = nums.length;
    // 1. 如果不在这个范围之内,就可以直接return-1了。
    if (target < nums[0] || target > nums[n - 1]) {
        return -1;
    }
    return recursion(nums, 0, n - 1, target);
}

public int recursion(int[] nums, int first, int last, int target) {
    if (first > last) {
        return -1;
    }
    // 2. 防止int + int 溢出
	int mid = first + ((last - first) >> 1);
    if (nums[mid] > target) {
        return recursion(nums, first, mid - 1, target);
    }
    if (nums[mid] < target) {
        return recursion(nums, mid + 1, last, target);
    }
    return mid;
}

别小瞧这些细节小优化,你写出来了,你就比别人好一点点。很多个一点点,就是许多了!

1-3:迭代解法

public int searchIteration(int[] nums, int target) {
    int n = nums.length;
    if (target < nums[0] || target > nums[n - 1]) {
        return -1;
    }

    int first = 0;
    int last = nums.length - 1;
    while (first <= last) {
        int mid = first + (last - first) / 2;
        if (nums[mid] > target) {
            last = mid - 1;
        } else if (nums[mid] < target) {
            first = mid + 1;
        } else {
            return mid;
        }
    }
    return -1;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

河海哥yyds

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值