Easy to understand python solution beats 99.89%


  • 0
    S

    It's actually quadratic.

        def threeSum(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            if len(nums) < 3:
                return []
            s = sorted(nums)
            i = 0
            j = 1
            r = []
            while i < j:
                j = i + 1
                k = len(s) - 1
                while j < k:
                    v = s[i] + s[j] + s[k]
                    if v > 0:
                        k -= 1
                        while j < k and s[k] == s[k+1]:
                            k -= 1
                    else:
                        if v == 0:
                            r.append([s[i],s[j],s[k]])
                        j += 1
                        while j < k and s[j] == s[j-1]:
                            j += 1
                i += 1
                while i < len(s) and s[i] == s[i-1]:
                    i += 1
            return r
    

Log in to reply
 

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