← 返回 bytedance 的题目列表Algorithm and Data Structure Challenge
类型:online_judge
Please implement a function that takes an array of integers and a target integer, and returns the indices of the two numbers such that they add up to the target integer.
Input
An integer array nums, where $2 \leq \text{len(nums)} \leq 10^4$, and each element is $-10^9 \leq ext{nums[i]} \leq 10^9$.
A target integer target, $-10^9 \leq ext{target} \leq 10^9$.
Output
If there exists such a pair of integers, return their indices as a list [i, j], where $0 \leq i < j < \text{len(nums)}$.
If no such pair exists, return an empty list.
Example
Example 1:
Input: nums = [2, 7, 11, 15], target = 9
Output: [0, 1]
Explanation: Because nums[0] + nums[1] == 9, return [0, 1].
Example 2:
Input: nums = [3, 2, 4], target = 6
Output: [1, 2]
Example 3:
Input: nums = [3, 3], target = 6
Output: [0, 1]
Constraints
Space complexity: O(n)
Time complexity: O(n)
Example
Input
2
7
11
15
9