Given an integer array, find all unique triplets that sum to zero.
Implement three_sum(nums), returning a list of triplets. Each triplet must contain values in nondecreasing order, and the result must not contain duplicate triplets. The result order should follow the sorted traversal produced by the standard two-pointer approach.
Input is a list of integers. The function returns a list of three-element lists. Return an empty list when no valid triplet exists.
def three_sum(nums):