Java implements LeetCode 704 binary search (detailed explanation of three solutions)


704.Binary search _

Given an n-element ordered (ascending) integer array nums and a target value target, write a function to search for target in nums and return the subscript if the target value exists, otherwise -1.

Example 1:

Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 appears in nums with subscript 4
Example 2:

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

hint:

You can assume that all elements in nums are unique.
n will be between [1, 10000].
Each element of nums will be between [-9999, 9999].

1. Dichotomy - basic version (left closed and right closed interval)

class Solution {
    
    
    public int search(int[] nums, int target) {
    
    
        int i = 0,j = nums.length - 1;
        while(i <= j){
    
    
            int m = (i + j) >>> 1;
            if(target < nums[m]){
    
    
                j = m - 1;
            }else if(nums[m] < target){
    
    
                i = m + 1;
            }else{
    
    
                return m;
            }
        }
        return -1;
    }
}
  • i and jpoint to the starting and ending positions of the array respectively
  • The loop condition is that i <= jas long as the array has not been traversed, the search can continue
  • Use bit operations >>> 1to implement division 2operations to prevent right + leftexceeding intthe range of
  • If the target value target is less than the middle value nums[m], it means that the target value is to the left of the middle value, jupdate the end position to m - 1, and continue searching for the left half
  • If the middle value nums[m] is less than the target value target, it means that the target value is to the right of the middle value, iupdate the starting position to m + 1, and continue searching for the right half
  • If the intermediate value nums[m]is equal to the target value target, it means that the target value is found and the index is returned directly.m
  • If the target value is still not found at the end of the loop, it returns -1, indicating that the target value does not exist in the array.

2. Dichotomy - improved version (left closed and right open interval)

class Solution {
    
    
    public int search(int[] nums, int target) {
    
    
        int i = 0,j = nums.length;
        while(i < j){
    
    
            int m = (i + j) >>> 1;
            if(target < nums[m]){
    
    
                j = m;
            }else if(nums[m] < target){
    
    
                i = m + 1;
            }else{
    
    
                return m;
            }
        }
        return -1;
    }
}
  • i and jrespectively point to 起始位置the sum of the arrays右边界
  • Use bit operations >>> 1to implement division 2operations to prevent right + leftexceeding intthe range of
  • The loop condition is that i < jas long as the array has not been traversed, the search can continue
  • If the target integer is less than nums[m], it means that the target integer is on mthe left side of , jadjust to m
  • If the target integer is greater than nums[m], it means that the target integer is on mthe right side of , iadjust tom + 1
  • If the target integer is equal to the target integer nums[m], it means that the target integer is found and the index is returned directly.m
  • If the target integer is not found at the end of the loop, return -1means not found

3. Dichotomy - balanced version (left closed and right open interval)

class Solution {
    
    
    public int search(int[] nums, int target) {
    
    
        int i = 0,j = nums.length;
        while(i + 1 < j){
    
    
            int m = (i + j) >>> 1;
            if(target < nums[m]){
    
    
                j = m;
            }else{
    
    
                i = m;
            }
        }
        return (nums[i] == target) ? i : -1;
    }
}
  • The loop condition is i + 1 < jthat the starting position pointer iis always jsmaller than the end position pointer 1, ensuring that in the last loop, the pointer i points to the last possible position of the target integer.
  • If the target integer is found, ithe value of the pointer is returned, otherwise it is returned -1to indicate that it was not found.

Guess you like

Origin blog.csdn.net/weixin_61370021/article/details/131532176