#18
4Sum
expert · 1015 · lc medium +32 · verified · 40.1% accepted · 12,788 likes · top 19%
Description
From array nums of n integers, find every unique quadruplet [nums[a], nums[b], nums[c], nums[d]] where all four indices are distinct, 0 <= a, b, c, d < n, and the four values sum to target. Results may appear in any order.
Example 1:
Input: nums = [1,0,-1,0,-2,2], target = 0
Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
Example 2:
Input: nums = [2,2,2,2,2], target = 8
Output: [[2,2,2,2]]
Code
1
2
3