LeetCode 题解工作台

两数之和 II - 输入有序数组

给你一个下标从 1 开始的整数数组 numbers ,该数组已按 非递减顺序排列 ,请你从数组中找出满足相加之和等于目标数 target 的两个数。如果设这两个数分别是 numbers[index 1 ] 和 numbers[index 2 ] ,则 1 1 2 。 以长度为 2 的整数数组 [in…

category

3

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

中等 · 二分·搜索·答案·空间

bolt

答案摘要

我们注意到数组按照非递减顺序排列,因此对于每个 ,可以通过二分查找的方式找到 $target - numbers[i]$ 的位置,如果存在,那么返回 $[i + 1, j + 1]$ 即可。 时间复杂度 $O(n \times \log n)$,其中 为数组 的长度。空间复杂度 。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 二分·搜索·答案·空间 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个下标从 1 开始的整数数组 numbers ,该数组已按 非递减顺序排列  ,请你从数组中找出满足相加之和等于目标数 target 的两个数。如果设这两个数分别是 numbers[index1]numbers[index2] ,则 1 <= index1 < index2 <= numbers.length

以长度为 2 的整数数组 [index1, index2] 的形式返回这两个整数的下标 index1 index2

你可以假设每个输入 只对应唯一的答案 ,而且你 不可以 重复使用相同的元素。

你所设计的解决方案必须只使用常量级的额外空间。

 

示例 1:

输入:numbers = [2,7,11,15], target = 9
输出:[1,2]
解释:2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。返回 [1, 2] 。

示例 2:

输入:numbers = [2,3,4], target = 6
输出:[1,3]
解释:2 与 4 之和等于目标数 6 。因此 index1 = 1, index2 = 3 。返回 [1, 3] 。

示例 3:

输入:numbers = [-1,0], target = -1
输出:[1,2]
解释:-1 与 0 之和等于目标数 -1 。因此 index1 = 1, index2 = 2 。返回 [1, 2] 。

 

提示:

  • 2 <= numbers.length <= 3 * 104
  • -1000 <= numbers[i] <= 1000
  • numbers非递减顺序 排列
  • -1000 <= target <= 1000
  • 仅存在一个有效答案
lightbulb

解题思路

方法一:二分查找

我们注意到数组按照非递减顺序排列,因此对于每个 numbers[i]numbers[i],可以通过二分查找的方式找到 targetnumbers[i]target - numbers[i] 的位置,如果存在,那么返回 [i+1,j+1][i + 1, j + 1] 即可。

时间复杂度 O(n×logn)O(n \times \log n),其中 nn 为数组 numbersnumbers 的长度。空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        n = len(numbers)
        for i in range(n - 1):
            x = target - numbers[i]
            j = bisect_left(numbers, x, lo=i + 1)
            if j < n and numbers[j] == x:
                return [i + 1, j + 1]
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Candidates should understand the benefits of applying binary search to a sorted array.

  • question_mark

    Look for understanding of the two-pointer technique and when it is most useful.

  • question_mark

    The candidate should be able to discuss the space-time trade-offs between the different approaches.

warning

常见陷阱

外企场景
  • error

    Failing to recognize that the array is sorted, which leads to inefficient brute-force solutions.

  • error

    Not considering edge cases such as negative numbers or arrays with only two elements.

  • error

    Mixing up index-based positions with 0-indexed arrays—make sure indices are 1-based.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    What if the array is not sorted? The problem would require different approaches like using a hash map.

  • arrow_right_alt

    How would you solve the problem if there were multiple pairs that sum to the target? You’d need to modify your approach to find all pairs.

  • arrow_right_alt

    How would this problem change if the array contained repeated elements? You would need to ensure that no element is used more than once.

help

常见问题

外企场景

两数之和 II - 输入有序数组题解:二分·搜索·答案·空间 | LeetCode #167 中等