Algorithm routine-binary search

table of Contents

 

1 Return when found

2 Find the left border

3 Find the right boundary

4 Rotate the array

4.1 Find the minimum value in the rotated array

4.2 Find a specific value in the rotated array

 


The most error-prone binary search is the processing of boundary conditions. After encountering this problem, making mistakes and entangled many times, I finally made up my mind to overcome this problem, so that the next time I encounter the dichotomous problem, I can do it with ease.

1 Return when found

This is the simplest. If the median value is greater than the target, it will go to the left, if it is less than the target, it will go to the right, and it will be found when it is equal to the target.

Leetcode example: 69. Square root of x

2 Find the left border

For example, a=[2,3,3,4,6], find the first subscript with a value of 3.

Start with left=0, right=4, mid=2, find the 2 subscript for the first time,

right=2,left=0,mid=1, that is to say, I found a subscript with a value of 3. I don't know if it is the first one, so I put it on the right end.

If there is still a value of 3 in front, set the previous one to the right end, and then right=1

 

Termination conditions Some people are used to left<=right, so left=right+1 at the end of the loop. But I still like the form of left<right, so that when the loop ends, left==right, and then judge whether nums[left] or nums[right] is the target, if it is, it will be found, or the target does not exist.

class Solution:
    def minEatingSpeed(self, nums: List[int], target: int) -> int:
        left,right=1,len(nums)-1
        while left<right:
            mid=(left+right)//2#mid值偏左
            if nums[mid]>=target: 
              	right=mid#因为找左边界,对可能的右边界就宽容
            else: 
                left=mid+1
        return left

Leetcode example questions:

278. The first wrong version

875. Koko Who Loves Bananas

34. Find the first and last position of an element in a sorted array

3 Find the right boundary

class Solution:
    def minEatingSpeed(self, nums: List[int], target: int) -> int:
        left,right=1,len(nums)-1
        while left<right:
            mid=(left+right+1)//2#mid值偏右
            if nums[mid]<=target: 
              	left=mid#因为找右边界,对可能的左边界就宽容
            else: 
                right=mid-1
        return left

34. Find the first and last position of an element in a sorted array

4 Rotate the array

4.1 Find the minimum value in the rotated array

Ideas:

If nums[mid]<nums[right], it means that the right side is ordered, and the minimum value is not on the mid but on the left, so right=mid

If nums[mid]>nums[right], it means that the left side is in order, the minimum value is on the right, left=mid+1

If nums[mid]==nums[right], you cannot judge which side is in order, right-=1

 

No repetition

153. Find the smallest value in a rotated sorted array

There are duplicates

154. Find the smallest value in a rotated sorted array II

4.2 Find a specific value in the rotated array

81. Search Rotated Sorted Array II

Ideas:

  • If nums[mid]<nums[right], it means that the right is in order, and the target is on the left if it is not on the right

  • If nums[mid]>nums[right], it means that the left side is ordered, and the target is not on the left, but on the right

  • If nums[mid]==nums[right], you cannot judge which side is in order, right-=1

#找旋转数组中的特定值是否存在代码 
class Solution:
    def search(self, nums: List[int], target: int) -> bool:
        left,right=0,len(nums)-1
        mid=0
        if len(nums)==0:return False
        while left<right:
            mid=(left+right)//2
            if nums[mid]==target:return True
            if nums[mid]<nums[right]:
                if target>nums[mid] and target<=nums[right]:
                    left=mid+1
                else:
                    right=mid-1
            elif nums[mid]>nums[right]:
                if target>=nums[left] and target<nums[mid]:
                    right=mid-1
                else:
                    left=mid+1
            else:
                right-=1
        return nums[right]==target

 

Guess you like

Origin blog.csdn.net/nanfeizhenkuangou/article/details/110634491