python solution with O(n^3)


  • 0
    L
        def fourSum(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: List[List[int]]
            """
            a=[]
            if len(nums)<=3:
                return []
            elif any(nums)==0 and target==0:
                return [[0,0,0,0]]
            else:
                for i in range(len(nums)-2):
                    for j in range(i+1,len(nums)):
                        buff={}
                        for k in range(j+1,len(nums)):
                            if nums[k] in buff:
                                b=[nums[i],nums[buff[nums[k]]],nums[j],nums[k]]
                                b.sort()
                                a.append(tuple(b))
                            else:
                                buff[target-nums[i]-nums[j]-nums[k]]=k
            return list(set(tuple(a)))

Log in to reply
 

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