class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
# Sort the array
nums.sort()
result = []
for i in range(len(nums)):
# Skip duplicate elements
if i > 0 and nums[i] == nums[i - 1]:
continue
left, right = i + 1, len(nums) - 1
while left < right:
total = nums[i] + nums[left] + nums[right]
if total < 0:
# Move the left pointer to the right
left += 1
elif total > 0:
# Move the right pointer to the left
right -= 1
else:
# Found a triplet
result.append([nums[i], nums[left], nums[right]])
# Skip duplicates for left and right pointers
while left < right and nums[left] == nums[left + 1]:
left += 1
while left < right and nums[right] == nums[right - 1]:
right -= 1
# Move the pointers
left += 1
right -= 1
return result