二分法(binary search)系列

2021/10/24 6:09:53

本文主要是介绍二分法(binary search)系列,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

35. Search Insert Position

Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

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

Example 1:

Input: nums = [1,3,5,6], target = 5
Output: 2

Example 2:

Input: nums = [1,3,5,6], target = 2
Output: 1

Example 3:

Input: nums = [1,3,5,6], target = 7
Output: 4

Example 4:

Input: nums = [1,3,5,6], target = 0
Output: 0

Example 5:

Input: nums = [1], target = 0
Output: 0

Constraints:

  • 1 <= nums.length <= 104
  • -104 <= nums[i] <= 104
  • nums contains distinct values sorted in ascending order.
  • -104 <= target <= 104

解法一:这种解法可以用于处理一些简单的二分法问题

class Solution {
    public int searchInsert(int[] nums, int target) {
        int l=0,r=nums.length-1;
        while(l+1<r){
            int mid = l+(r-l)/2;
            if(nums[mid]==target) return mid;
            else if(nums[mid]>target) r=mid;
            else l=mid+1;
        }
        if(nums[l]>=target) return l;
        else if(nums[r]>=target) return r;
        else return r+1;
    }
}

解法二:

l 和 r 所定义的出的数组范围为 [l, r), 是 左闭右开 的 也就是在后续的循环中, r 所指向的位置是 不被 包括在循环以内的, r 所代表的位置实际上是要查找的数组的最后一个元素的后一个元素。

因为是 左闭右开 的 r 初值为 nums.size() ,因为数组的最后一个元素的索引为 nums[nums.length - 1], 根据 r 定义最后一个元素的后一个元素即为 r = nums.length;
因为是 左闭右开 的循环结束条件的判断中为 while(l < r) 因为对于左闭右开的区间 [2, 2) 这种数值是无意义的, 所以当 r = l 的时候, 就该结束循环了, 所以只有在 l < r 才继续循环
因为是 左闭右开 的 r 的移动规则为 r = mid ,因为当前循环查找的为索引为 mid 位置的元素(即:(nums[mid] == target)), 下一次应该将查找范围的右边界设置为 mid 位置的前一个元素([l, m - 1]), 因为 r 指向最后一个元素的后一个元素, 当 r = m , 下次的查找范围就为 [l, r)即 [l, m - 1]

class Solution {
    public int searchInsert(int[] nums, int target) {
        int l=0,r=nums.length;
        while(l<r){
            int mid = l+(r-l)/2;
            if(nums[mid]==target) return mid;
            else if(nums[mid]>target) r=mid;
            else l=mid+1;
        }
        return l;
    }
}

解法三:

常规写法 1 中 l 和 r 的定义的范围为 [l, r],是 左闭右闭 的也就是在后续的循环中, r 所指向的位置是 被 包括在循环以内的, r 所代表的位置实际上是要查找的数组的最后一个元素。

因为是 左闭右闭 的 r 初值应为 nums.length - 1 ,因为数组的最后一个元素的索引为 nums[nums.length - 1], 根据 r 定义 最后一个元素 即为 r = nums.length - 1;
因为是 左闭右闭 循环结束条件的判断中为 while(l < r) ,因为对于左闭右闭的区间 [2, 2] 这种数值是有意义的(包含元素 2), 所以当 r = l 的时候, 还有一个元素应该去查找, 所以 l <= r 继续循环
因为是 左闭右闭 r 的移动规则为 r = mid - 1 ,因为当前循环被查找的为索引为 m 位置的元素(即:(nums[mid] == target)) , 下一次应该将查找范围的右边界设置为 m 位置的前一个元素([l, mid - 1]), 因为 r 指向最后一个元素 , 所以让 r = mid - 1 , 下次的查找范就为 [l, r - 1] 即 [l, mid - 1]

class Solution {
    public int searchInsert(int[] nums, int target) {
        int l=0,r=nums.length-1;
        while(l<=r){
            int mid = l+(r-l)/2;
            if(nums[mid]==target) return mid;
            else if(nums[mid]>target) r=mid-1;
            else l=mid+1;
        }
        return l;
    }
}

 

33. Search in Rotated Sorted Array

There is an integer array nums sorted in ascending order (with distinct values).

Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].

Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.

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

Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4

Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1

Example 3:

Input: nums = [1], target = 0
Output: -1 

Constraints:

  • 1 <= nums.length <= 5000
  • -104 <= nums[i] <= 104
  • All values of nums are unique.
  • nums is an ascending array that is possibly rotated.
  • -104 <= target <= 104
class Solution {
    public int search(int[] nums, int target) {
        int len = nums.length;
        if(nums[len-1]==target) return len-1;
        //1.find the switch point
        int l=0,r=len-1;
        while(l<r){
            int mid = l+(r-l)/2;
            if(nums[mid]>nums[len-1]) l=mid+1;
            else r=mid;
        }
        int pivot = r;
        //2.base on the switch point to do binarysearch
        if(nums[len-1]<target){
            l = 0;
            r = pivot;
        }
        else{
            l=pivot;
            r = len;
        }
        //3.do binarysearch in the sorted arr
        while(l<r){
            int mid = l+(r-l)/2;
            if(nums[mid]>target) r=mid;
            else if(nums[mid]<target) l=mid+1;
            else
                return mid;
        }
        return -1;
    }
}

 



这篇关于二分法(binary search)系列的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程