Concise python solution


  • 1
    H
    class Solution:
        # @return a list of lists of length 3, [[val1,val2,val3]]
        def threeSum(self, num):
            num = sorted(num)
            i,result = 0, set()
            while i < len(num) - 2:
                j, k = i + 1, len(num) - 1
                while j < k:
                    if num[i] + num[j] + num[k] == 0:
                        result.add((num[i], num[j],num[k]))
                        j += 1
                    elif num[i] + num[j] + num[k] > 0:
                        k -= 1
                    else:
                        j += 1
                i+=1
            
            return [list(t) for t in result]

Log in to reply
 

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