1.1.1 Array - Search Insertion Position (Leetcode 35)

1. Topic

Leetcode link
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 time complexity O(log n).

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

hint:

  • 1 <= nums.length <= 104
  • -104 <= nums[i] <= 104
  • nums is an array in ascending order without repeated elements
  • -104 <= target <= 104

2. Idea

The title prompts to use dichotomy:

  • nums is an array in ascending order without repeated elements
  • Must use an algorithm with time complexity O(log n)

Four situations:

  • The target value precedes all elements of the array
  • The target value is equal to an element in the array
  • The position in the array at which the target value is inserted
  • The target value is after all elements of the array

key point:

  • When the binary method is used to end the traversal, the position pointed by left and right

3. Code implementation

3.1 Solution 1: Violent solution

  • Does not satisfy the condition of time complexity O(log n)
class Solution {
    
    
public:
    int searchInsert(vector<int>& nums, int target) {
    
    
        for(int i = 0; i < nums.size(); i++){
    
    
            // 对应于前三种情况
            if(nums[i] >= target){
    
    
                return i;
            }

        }
        // 对应于目标值在数组所有元素之后的情况
        return nums.size();

    }
};

3.2 Solution 2: dichotomy (left closed and right closed)

class Solution {
    
    
public:
    int searchInsert(vector<int>& nums, int target) {
    
    
       // 定义左闭右闭区间
       
       int left = 0;
       int right = nums.size() - 1;

        while(left <= right){
    
    
            int middle = left + (right - left) / 2;
            if(nums[middle] < target){
    
    
                left = middle + 1; // target 在右区间,所以[middle + 1, right]
            }else if(nums[middle] > target){
    
    
                right = middle - 1; // target 在左区间,所以[left, middle - 1]
            }else{
    
    // nums[middle] == target
                return middle;
            }
        }

        // 数组中不存在target
        // 1. 插入到所有元素之前,所有的元素都比target大:right = -1(left = 0)
        // 2. 插入到数组中,nums[left] < target < nums[right]
        // 下一次循环,执行left = middle + 1;
        // 下一次循环,执行right = middle - 1, 结束循环;
        // 3. 插入到所有元素之后,所有的元素都比target大:right = nums.size() - 1(left = nums.size())
        return right + 1;

    }
};

3.3 Solution 3: dichotomy (left closed and right open)

class Solution {
    
    
public:
    int searchInsert(vector<int>& nums, int target) {
    
    
       // 定义左闭右开区间
       
       int left = 0;
       int right = nums.size();

        while(left < right){
    
    
            int middle = left + (right - left) / 2;
            if(nums[middle] < target){
    
    
                left = middle + 1; // target 在右区间,所以[middle + 1, right)
            }else if(nums[middle] > target){
    
    
                right = middle; // target 在左区间,所以[left, middle)
            }else{
    
    // nums[middle] == target
                return middle;
            }
        }

        // 数组中不存在target
        // 1. 插入到所有元素之前,所有的元素都比target大:right = 0(left = 0)
        // 2. 插入到数组中,nums[left] < target < nums[right]
        // 下一次循环,执行left = middle + 1;
        // 下一次循环,执行right = middle, 结束循环;
        // 3. 插入到所有元素之后,所有的元素都比target大:right = nums.size()(left = nums.size())
        return right;

    }
};

4. Summary

  • It is necessary to deepen the understanding of the dichotomy process, especially when the loop ends, the pointing position of left and right

Guess you like

Origin blog.csdn.net/weixin_46297585/article/details/123195584