python solution


  • 0
    J
    class Solution(object):
        def sort(self, nums):
            dic = {}
            for i, x in enumerate(nums):
                if dic.get(x) is None:
                    dic[x] = 1
                elif dic.get(x) < 3:
                    dic[x] += 1
            nums, mnums = [], []
            for k in dic.keys():
                c = dic.get(k)
                for i in range(c):
                    if k >= 0:
                        nums.append(k)
                    else:
                        mnums.append(k)
            return mnums + nums
            
        def threeSum(self, nums):
            result = []
            nums = self.sort(nums)
                        
            for i, x in enumerate(nums):
                for j, y in enumerate(nums[i+1:]):
                    for z in nums[i+j+2:]:
                        if x + y + z == 0:
                            each = [x,y,z]
                            if not (each in result):
                                result.append(each)
                            break
                    if x + y >= 0:
                        break
                if x >= 0:
                    break
            return result
    

Log in to reply
 

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