Algorithms (Binary Search)

Preface: The time complexity of finding elements in an array is O(n). This blog mainly learns a method for efficiently finding elements: binary search, which can only be used in ordered arrays, and uses the idea of ​​​​divide and conquer. Instead of finding the end from the end, it defines a range, and then continuously narrows the search range, and finally finds the target element. The time complexity is O(log(n)).

mind Mapping

Rico 704

The classic problem of binary search, given an ordered array, find the specified element

Leek

Python code to solve the problem:

1. Variables that define the left and right boundaries, the left subscript is 0, and the right is the subscript value of the last element of the array, len(nums)-1

2. Add a layer of loop, if the left subscript is less than or equal to the right subscript, left<=right, then find a middle subscript, mid = left + (right - left) / 2

3. Compare the value corresponding to the middle subscript found in the array with the target element. If they are equal, return directly. The found value, if nums[mid] == target: return mid

4. If the value corresponding to the found middle subscript in the array is too large, search on the left side, and the right boundary changes, right = mid - 1

On the contrary, go to the right to find, the left boundary changes, left = mid + 1

5. If the left boundary is greater than the right boundary, left>right, and it has not been found, then return -1 directly

Things to pay attention to:

In step 2, the mid = left + (right - left) / 2 line of code, why not replace it with mid = (right + left) / 2?

This way of writing may have the risk of overflow. If the length of the array is very long, the left will keep approaching the value of right, and the length of int is limited. At this time, the value of left+right may appear negative. Use mid = left + (right - left ) / 2, effectively avoiding this risk, the result is also correct, and the calculated value is also half of the left and right boundaries.

class Solution(object):
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        left = 0
        right = len(nums) -1 
        while left <= right :
            mid = left + (right - left)/2
            if nums[mid] == target:
                return mid
            elif nums[mid] < target:
                left = mid + 1
            elif nums[mid] > target:
                right = mid - 1
        return -1

Leetcode 69 Calculate the square root

Leek

Python code to solve the problem:

class Solution(object):
    def mySqrt(self, x):
        """
        :type x: int
        :rtype: int
        """
        if x == 0 or x == 1:
            return x
        left = 1
        right = x
        while left <= right :
            mid = (left + right)//2
            if mid == x / mid:
                return mid
            elif mid > x / mid:
                right = mid - 1
            else:
                left = mid + 1

        if left  > x / left:
            return left - 1
        else:
            return left

Guess you like

Origin blog.csdn.net/MRJJ_9/article/details/132088670