Leetcode brushing record (3): 35 search insertion position

Brush questions website: Leetcode

Difficulty: easy

Language: Python

Plan : From easy -> to medium -> to hard.

1. 35 search insertion position

1.1 Problem description

Given a sorted array and a target value, find the target value in the array and return its index. If the target value does not exist in the array, returns the position where it will be inserted in order.

Please use an algorithm with O(log n) time complexity .

  • Example 1
输入:nums = [1,3,5,6],target=5
输出:2
  • Example 2
输入:nums = [1,3,5,6],target=2
输出:1
  • Example 3
输入:nums = [1,3,5,6],target=7
输出:4
  • Example 4
输入:nums = [1,3,5,6],target=0
输出:0
  • Example 5
输入:nums = [1],target=0
输出:0

1.2 Thinking and Analysis

From the title description, it is very similar to the binary search of an array , but the attention is a little different. That question is required for a value that is not in the array 返回-1, and this question is the corresponding insertion position.

In fact, it is not difficult to find that the point is targetthat when it is not in the array, it is compared to the last smaller number (that is, the same position index + 1) or the last larger number (that is, the same position index).

The number of the final ratio must be the number closest to the target, that is, the insertion position is either in front of this number or after this number plus 1.

For example, when Example 2 is brought into the operation, it can be seen that at the target=2end, it is compared with the smaller number 1 at the index position 0, so the corresponding position should be increased by 1, that is, 0+1.

For example, when Example 4 is brought into the operation, it can be seen that at the target=0end, it is compared with the larger number 1 at the index position 0, so the corresponding position should be the same position, that is, 0.

After understanding the basic algorithm idea, the previous algorithm is the same as the binary search of the array. We brought in several operations and found that in fact, the last position is leftthe returned value, that is, the inserted position index value, so we only need to add the return value at the end. index position, i.e.

return left

So the complete code should be:

class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        left = 0
        num = len(nums) - 1
        while left<=num:
            mid = left + (num-left)//2
            if target == nums[mid]:
                return mid
            elif target>nums[mid]:
                left = mid + 1
            else:
                num = mid - 1
        return left

insert image description here

1.3 Summary

This question is similar to the previous binary search of the array, but it is necessary to pay attention to the relationship between the position that should be inserted and the last comparison number that is not in the array.

Guess you like

Origin blog.csdn.net/A33280000f/article/details/121149906