Three Sum
MediumPython 3ArraysTwo pointersSorting
You are given an array of integers nums and an integer target. Return all unique triplets (a, b, c) drawn from three DISTINCT positions of nums whose values sum to target.
Two triplets are considered the same if they contain the same multiset of values, so the result must not contain duplicates. Return each triplet with its values in non-decreasing order, and the list of triplets sorted.
Constraints: - 0 <= len(nums) - values may be negative, zero, or positive, and may repeat
Examples:
nums = [-1, 0, 1, 2, -1, -4], target = 0
-> [(-1, -1, 2), (-1, 0, 1)]
nums = [0, 0, 0, 0], target = 0
-> [(0, 0, 0)] # only one unique triplet despite duplicates
nums = [1, 2, 3], target = 100
-> [] # no triplet reaches the target
nums = [-2, 0, 1, 1, 2], target = 0
-> [(-2, 0, 2), (-2, 1, 1)]Python 3, run in your browser. Run tests runs the problem's own tests with a free account; the AI interviewer reads your code and your runs in a practice interview.
Loading the editor…