Leetcode 15. 3Sum(python)
先排序,再循环遍历,用双指针。
class Solution(object): def threeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ solution=[] nums.sort() for i in range(len(nums)-1): left=i+1 right=len(nums)-1 while left<right: val=nums[i]+nums[left]+nums[right] if val==0 and [nums[i],nums[left],nums[right]] not in solution: solution.append([nums[i],nums[left],nums[right]]) left+=1 right-=1 elif val<0: left+=1 else: right-=1 return solution