Four Sum
Problem statement
You are given an integer array nums and a target. Find every group of four values whose sum equals target. The four values must come from four different array positions.
For this exercise, sort the four values in each group from smallest to largest, and sort the result list lexicographically. Return each group of values only once, even if several choices of positions produce it. Return an empty list if there is no such group. You may change the input array.
Function
fourSum(nums: int[], target: long) → int[][]Examples
Example 1
nums = [1,0,-1,0,-2,2]target = 0return = [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]These are the three distinct sorted value quadruplets with sum zero. Their indices are distinct within each selection.
Example 2
nums = [2,2,2,2,2]target = 8return = [[2,2,2,2]]Several index selections exist, but all produce the same value quadruplet, which appears once.
Constraints
- For this exercise, assume
0 <= nums.length <= 200. -10^9 <= nums[i] <= 10^9.-4 * 10^9 <= target <= 4 * 10^9; use 64-bit sums.