← 返回 google 的题目列表Two Sum with Three Sum Follow-up
类型:online_judge
Two Sum with Three Sum Follow-up
Given an integer array nums and an integer target:
Implement twoSum(nums, target): return two distinct indices [i, j] such that nums[i] + nums[j] == target. Assume exactly one valid answer exists, and an element cannot be reused.
Follow-up: implement threeSum(nums), returning all unique triplets [a, b, c] such that a + b + c == 0. No duplicate triplets may appear.
Examples
Two Sum:
nums = [2, 7, 11, 15], target = 9
Output: [0, 1]
Three Sum:
nums = [-1, 0, 1, 2, -1, -4]
Output: [[-1, -1, 2], [-1, 0, 1]]
Constraints
2 <= len(nums) <= 10^4 for Two Sum.
3 <= len(nums) <= 3 * 10^3 for Three Sum.
Values are in [-10^9, 10^9].
Example
Input
two_sum
4 9
2 7 11 15
Output
0 1