35. (Search Insert Position)搜索插入位置

Posted OIqng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.

给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。

请必须使用时间复杂度为 O(log n) 的算法。

Example 1:

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

示例 1:

输入: nums = [1,3,5,6], target = 5
输出: 2

Example 2:

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

示例 2:

输入: nums = [1,3,5,6], target = 2
输出: 1

Example 3:

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

示例 3:

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

Example 4:

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

示例 4:

输入: nums = [1,3,5,6], target = 0
输出: 0

Example 5:

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

示例 5:

输入: nums = [1], target = 0
输出: 0

Constraints:

  • 1 <= nums.length <= 10 4 ^4 4
  • -10 4 ^4 4<= nums[i] <= 10 4 ^4 4
  • nums contains distinct values sorted in ascending order.
    –10 4 ^4 4 <= target <= 10 4 ^4 4

提示:

  • 1 <= nums.length <= 10 4 ^4 4
  • -10 4 ^4 4<= nums[i] <= 10 4 ^4 4
  • nums 为无重复元素的升序排列数组
  • -10 4 ^4 4 <= target <= 10 4 ^4 4

解题思路

方法:二分查找

当题目中只要求我们找到一个排序数组中的一个目标值并返回其索引值时,我们第一想到的就是二分查找。然而题目中还有一个条件当这个目标值不在时,返回按顺序插入的索引值。

我们使用position来定义插入的位置时:
满足条件nums[positon-1] < < < target ≤ \\leq nums[position](即在一个有序数组中找第一个大于target值的索引值,返回按顺序插入的索引值。

Python代码

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

Java代码

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

C++代码

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int n = nums.size();
        int left = 0,right = n-1;
        while(left <= right){
            int mid = left + (right - left) / 2;
            if(target <= nums[mid]){
                right = mid - 1;
            }
            else{
                left = mid + 1;
            }
        }
        return left;

    }
};

复杂度分析

时间复杂度:O( l o g n log_n logn),其中 n为数组的长度。二分查找所需的时间复杂度为 O( l o g n log_n logn)。

空间复杂度:O(1)。我们只存放若干变量。

以上是关于35. (Search Insert Position)搜索插入位置的主要内容,如果未能解决你的问题,请参考以下文章

35. Search Insert Position

Search Insert Position

Search Insert Position

Search Insert Position

LC.35.Search Insert Position

#LeetCode# 35. Search Insert Position