15. 3Sum


class Solution(object):
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        if len(nums)<3:
            return []
        nums.sort()
        res=set()
        for i,v in enumerate(nums[:-2]):
            if i>0 and v==nums[i-1]:
                continue
            d={}
            for x in nums[i+1:]:
                if x not in d:
                    d[-v-x]=2;
                else:
                    res.add((v,-v-x,x))
        return map(list,res)
            

你可能感兴趣的:(15. 3Sum)