Simple python solution using sets (5 lines)


  • 0
    M
        def subsetsWithDup(self, nums):
            res = {()}
            for num in sorted(nums):
                for item in res.copy():
                    res.add(item+(num,))
            return [list(item) for item in res]
    

    Note: It seems converting tuples to list and back on 3rd line might make it a bit faster:

                    res.add(tuple(list(item)+[num]))
    

Log in to reply
 

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