Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
defthreeSum(self, nums: List[int]) -> List[List[int]]: nums.sort() res = [] for i in range(len(nums)): if i > 0and nums[i] == nums[i - 1]: continue sub_res = self.two_sum(nums, i + 1, -nums[i]) for li in sub_res: res.append([nums[i]] + li) return res
deftwo_sum(self, nums, start, target): j, k, res = start, len(nums) - 1, [] while j < k: if (j > start and nums[j] == nums[j - 1]) or nums[j] + nums[k] < target: j += 1 elif (k < len(nums) - 1and nums[k] == nums[k + 1]) or nums[j] + nums[k] > target: k -= 1 elif nums[j] + nums[k] == target: res.append([nums[j], nums[k]]) j += 1 k -= 1 return res