classSolution: defthreeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ mymap = {} results = set() for i inrange(len(nums)): for j inrange(i + 1, len(nums)): if - nums[i] - nums[j] in mymap: a = [- nums[i] - nums[j], nums[i], nums[j]] a.sort() results.add(tuple(a)) mymap[nums[i]] = i re = [] for a in results: re.append(list(a)) return re
classSolution: defthreeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ nums.sort() mymap = set() results = set() for i inrange(len(nums)): if i > 1and nums[i] == nums[i-2]: continue for j inrange(i + 1, len(nums)): if j != i + 1and nums[j] == nums[j-1]: continue if - nums[i] - nums[j] in mymap: a = [- nums[i] - nums[j], nums[i], nums[j]] a.sort() results.add(tuple(a)) mymap.add(nums[i]) re = [] for a in results: re.append(list(a)) return re
classSolution: defthreeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ nums.sort() results = [] for i inrange(len(nums)): # never let i refer to the same value twice if i != 0and nums[i] == nums[i-1]: continue j = i + 1 k = len(nums) - 1 while j < k: if nums[i] + nums[j] + nums[k] == 0: results.append([nums[i], nums[j], nums[k]]) j += 1 k -= 1 while j < k and nums[j] == nums[j-1]: # never let j refer the same value twice j += 1 elif nums[i] + nums[j] + nums[k] < 0: j += 1 else: k -= 1 return results