classSolution:defthreeSum(self,nums: List[int]) -> List[List[int]]: ans = [] nums.sort()for first inrange(len(nums) -2):if first >0and nums[first]== nums[first -1]:# 第一个指针, 只考虑重复数字中的第一个continue third =len(nums)-1 target =-nums[first]for second inrange(first +1, len(nums) -1):if second > first +1and nums[second]== nums[second -1]:# 第二个指针, 只考虑第一个指针后, 重复数字中的第一个continue second_num = nums[second]while third > second and second_num + nums[third]> target: third -=1if third <= second:breakif second_num + nums[third]== target: ans.append([nums[first], second_num, nums[third]])return ans