3Sum solution in python


  • 0
    Q
    class Solution:
        def threeSum(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            a=[]
            b = set()
            l = 0
            q = 0
            for i in range(len(nums)):
                for j in range(i+1,len(nums)):
                    for k in range(j+1,len(nums)):
                        if nums[i]+nums[j]+nums[k] == 0:
                            if l == 0:
                                a.append([nums[i], nums[j], nums[k]])
                                l +=1
                            else:
                                q = 0
                                for z in a:
                                    for x in z:
                                        b.add(x)
                                    if b == {nums[i],nums[j],nums[k]}:
                                        q +=1
                                    b = set()
                                if q == 0:
                                    a.append([nums[i], nums[j], nums[k]])
            return (a)
    

Log in to reply
 

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