Solve the 3-Sum problem, optimizing your solution to run in O(N^2) time complexity.
Implement def three_sum(nums):. Given a list of integers, return every unique triplet [a, b, c] such that a + b + c == 0. The result must not contain duplicate triplets, and each triplet must be sorted in nondecreasing order. Return an empty list when no valid triplet exists.
def three_sum(nums):