classSolution(object): defthreeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ nums.sort() result = list() for i, a inenumerate(nums): # two pointers left = i + 1 right = len(nums) - 1 while left < right: if a + nums[left] + nums[right] == 0: _res = sorted([a, nums[left], nums[right]]) # check duplication if _res notin result: result.append(_res) # update pointers left += 1 right -= 1 elif a + nums[left] + nums[right] > 0: # nums[right] is larger right -= 1 else: # nums[left] is smaller left += 1 return result
classSolution(object): defthreeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ nums.sort() result = list() for i, a inenumerate(nums): # check and remove duplication on a if i > 0and a == nums[i-1]: continue # two pointers left = i + 1 right = len(nums) - 1 while left < right: if a + nums[left] + nums[right] == 0: result.append([a, nums[left], nums[right]]) # check and remove duplication on b and c while left < right and nums[left] == nums[left+1]: left += 1 while left < right and nums[right] == nums[right-1]: right -= 1 # update pointers left += 1 right -= 1 elif a + nums[left] + nums[right] > 0: # nums[right] is larger right -= 1 else: # nums[left] is smaller left += 1 return result
classSolution(object): defthreeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ nums.sort() result = list() for i, a inenumerate(nums): # bound condition if a > 0: return result # check and remove duplication on a if i > 0and a == nums[i-1]: continue # two pointers left = i + 1 right = len(nums) - 1 while left < right: if a + nums[left] + nums[right] == 0: result.append([a, nums[left], nums[right]]) # check and remove duplication on b and c while left < right and nums[left] == nums[left+1]: left += 1 while left < right and nums[right] == nums[right-1]: right -= 1 # update pointers left += 1 right -= 1 elif a + nums[left] + nums[right] > 0: # nums[right] is larger right -= 1 else: # nums[left] is smaller left += 1 return result