Fastest python solution I think


  • 0
    Y
    class Solution:
    # @return a list of lists of length 3, [[val1,val2,val3]]
    def threeSum(self, num):
        num.sort()
        res=[]
        i=0
        while i<len(num)-2:
            j, k = i+1, len(num) - 1
            while j < k:
                sum = num[i] + num[j] + num[k]
                if sum == 0:
                    res.append([num[i],num[j],num[k]])
                    j+=1
                    while j<k and num[j]==num[j-1]:
                        j+=1
                    k-=1
                    while j<k and num[k]==num[k+1]:
                        k-=1
                elif sum < 0:
                    j += 1
                elif sum > 0:
                    k -= 1
            i+=1
            while i<len(num)-2 and num[i]==num[i-1]:
                i+=1
        return res

  • 0
    W

    awesome!
    tks a lot


Log in to reply
 

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