返回介绍

Search Insert Position

发布于 2025-02-22 13:01:24 字数 2967 浏览 0 评论 0 收藏 0

Source

Problem

Given a sorted array 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 may assume NO duplicates in the array.

Example

[1,3,5,6] , 5 → 2

[1,3,5,6] , 2 → 1

[1,3,5,6] , 7 → 4

[1,3,5,6] , 0 → 0

Challenge

O(log(n)) time

题解

Python

问题可以转化为, 寻找 first position that value is >= target 。如果没找到, 那么就插入在 list 的尾部。

class Solution:
  """
  @param A : a list of integers
  @param target : an integer to be inserted
  @return : an integer
  """
  def searchInsert(self, A, target):
    if not A:
      return 0
    st, ed = 0, len(A) - 1
    while st + 1 < ed:
      mid = (st + ed) / 2
      if A[mid] == target:
        ed = mid
      elif A[mid] < target:
        st = mid
      else:
        ed = mid
    if A[st] >= target:
      return st
    elif A[ed] >= target:
      return ed
    else:
      return len(A)

Java

仍然是 Binary Searchlower_bound 的变形,两大关键点: startend 的初始化;最终插入位置和 start 以及 end 之间的关系,由于 start 对应的索引一定是小于目标值的,那么 start + 1 就是要求的值了,再检查下两端的边界,DONE

public class Solution {
  /**
   * param A : an integer sorted array
   * param target :  an integer to be inserted
   * return : an integer
   */
  public int searchInsert(int[] A, int target) {
    if (A == null || A.length == 0) {
      return -1;
    }

    int start = -1, end = A.length;
    while (start + 1 < end) {
      int mid = start + (end - start) / 2;
      if (A[mid] == target) {
        return mid; // no duplicates
      } else if (A[mid] < target) {
        start = mid;
      } else {
        end = mid;
      }
    }

  return start + 1;
  }
}

源码分析

分析三种典型情况:

  1. 目标值在数组范围之内,最后返回值一定是 start + 1
  2. 目标值比数组最小值还小,此时 start 一直为 -1 , 故最后返回 start + 1 也没错,也可以将 -1 理解为数组前一个更小的值
  3. 目标值大于等于数组最后一个值,由于循环退出条件为 start + 1 == end , 那么循环退出时一定有 start = A.length - 1 , 应该返回 start + 1

综上所述,返回 start + 1 是非常优雅的实现。其实以上三种情况都可以统一为一种方式来理解,即索引 -1 对应于在数组前方插入一个非常小的数,索引 end 即对应数组后方插入一个非常大的数,那么要插入的数就一定在 startend 之间了。

有时复杂的边界条件处理可以通过『补项』这种优雅的方式巧妙处理。

复杂度分析

时间复杂度 O(logn)O(\log n)O(logn), 空间复杂度 O(1)O(1)O(1).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文