code.davidloor.com
MMedium· two-pointers· array· sorting

3Sum

3Sum

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i, j, and k are distinct indices and nums[i] + nums[j] + nums[k] == 0.

The solution set must not contain duplicate triplets.

Example

Input: nums = [-1, 0, 1, 2, -1, -4] Output: [[-1, -1, 2], [-1, 0, 1]]

Constraints

  • 3 <= nums.length <= 3000
  • -10^5 <= nums[i] <= 10^5

Entry — threeSum

Grading — set_of_sets over 4 tests

Stuck? Browse algorithmic patterns → or read the Python / JS foundations.
Awaiting first run
Reference solution unlocks after a run & 60 s of work.