Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. The solution set must not contain duplicate triplets.
Example 1
Input: nums = [-4,-1,-1,0,1,2,3]
Output: [[-4,1,3],[-1,-1,2],[-1,0,1]]
Explanation: The three distinct triplets that sum to zero are -4 + 1 + 3 = 0, -1 + (-1) + 2 = 0 and -1 + 0 + 1 = 0. The two -1 values sit at different indices, so [-1,-1,2] is allowed, but [-1,0,1] is listed only once even though either -1 could be used.
Example 2
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Explanation: nums[0] + nums[1] + nums[2] = -1 + 0 + 1 = 0 and nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0 give the same triplet [-1,0,1], so it appears once. nums[0] + nums[3] + nums[4] = -1 + 2 + (-1) = 0 gives [-1,-1,2].
Example 3
Input: nums = [0,1,1]
Output: []
Explanation: The only possible triplet is 0 + 1 + 1 = 2, which is not 0, so no triplet qualifies.
3Sum is really *2Sum on a sorted array*, done `n` times. Fix one element `a`; then find pairs `(b, c)` in the remaining array with `b + c == -a`. On a sorted array that pair search is `O(n)` with two pointers.
Run checks all cases above. Submit evaluates all test cases.