A naive Python solution ..


  • 1
    G
    class Solution:
        # @param num, a list of integer
        # @return a list of lists of integer
        # 1:02
        def __init__(self):
            self.output = []
    
        def subsetsWithDup(self, S, pos = 0, temp = None):
            temp = temp or []
            S.sort()
    
            if temp not in self.output:
              self.output.append(temp[:])
    
            for i in range(pos, len(S)):
                temp.append(S[i])
                self.subsetsWithDup(S, i+1, temp)
                temp.pop()
    
            return self.output

  • 0
    C

    returns error for me:

    Runtime Error Message: Line 30: AttributeError: Solution instance has no attribute 'partition'
    Last executed input: {}, 0

    did you run this code?


  • 0
    G

    Just tried again and worked for me ... you are probably pasting the solution from another question? (you mentioned partition)


  • 0
    C

    works, never mind : )


  • 0
    L

    you know, searching in a list costs much time


  • 0
    G

    That's what makes it a naive solution, right? ;)
    You can easily speed things up by adding a hash / change the output from array to hash (if you don't need to preserve the order of the output)


Log in to reply
 

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