class Solution:
def threeSum(self, nums: list[int]) -> list[list[int]]:
if len(nums) < 3:
return []
ans = []
nums.sort()
for i in range(len(nums) - 2):
if i > 0 and nums[i] == nums[i - 1]:
continue
# Choose nums[i] as the first number in the triplet, then search the
# remaining numbers in [i + 1, n - 1].
l = i + 1
r = len(nums) - 1
while l < r:
summ = nums[i] + nums[l] + nums[r]
if summ == 0:
ans.append((nums[i], nums[l], nums[r]))
l += 1
r -= 1
while nums[l] == nums[l - 1] and l < r:
l += 1
while nums[r] == nums[r + 1] and l < r:
r -= 1
elif summ < 0:
l += 1
else:
r -= 1
return ans