Python solution


  • 0
    H
    class Solution:
        # @param {integer[]} nums
        # @return {integer[][]}
        def threeSum(self, nums):
            if len(nums)<3:return []
            res=list()
            nums.sort()
            length=len(nums)
            for i in xrange(length-2):
                j=i+1
                k=length-1
                while(j<k):
                    if nums[j]+nums[k]==-1*nums[i]:
                        res.append([nums[i],nums[j],nums[k]])
                        j=j+1
                        k=k-1
                    elif nums[j]+nums[k]<-1*nums[i]: j=j+1
                    else: k=k-1
            return [list(x) for x in set([tuple(y)for y in res])]

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.