Simple Python Solution

  • 0
    class Solution(object):
        def twoSum(self, target, nums):
            n = len(nums)
            ret, d = [], {}
            for i in xrange(n):
                if nums[i] in d:
                    ret.append([d[nums[i]], nums[i]])
                    d[target - nums[i]] = nums[i]
            return ret
        def threeSum(self, num):
            :type nums: List[int]
            :rtype: List[List[int]]
            if len(num) < 3:
                return []
            n, res = len(num), []
            for i in xrange(n):
                if num[i] > 0:
                for two in self.twoSum(-num[i], num[i+1:]):
                    if [num[i]] + two not in res:
                        res.append([num[i]] + two)
            return res

Log in to reply

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