Python solution, beats 100%


  • 0
    S
    def helper(self, res, nums, index, table):
            if index == len(nums):
                return res
          
            num = nums[index]
            if num in table:
                # index hash table with number, duplicate the 
                # array, append number to each element, add back 
                # into res
                for i in range(len(table[num])):
                    res.append(table[num][i][:])
                    res[len(res)-1].append(num)
                    table[num][i] = res[len(res)-1][:]
            else:
                # duplicate elements that exist in res, append 
                # number to each element, add back into res
                table[nums[index]] = []    
                for i in range(len(res)):
                    res.append(res[i][:])
                    res[len(res)-1].append(num)
                    table[num].append(res[len(res)-1])
                
            index += 1
            return self.helper(res, nums, index, table)
    
        def subsetsWithDup(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            table = {}
            res = [[]]
            nums.sort()
            return self.helper(res, nums, 0, table)

Log in to reply
 

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