4 lines Python

  • 0

    Compared to the situation when x is single in nums, the only difference when x has duplicated numbers is we can extend from [x] up to [x] * k to the subset in our current res.

    class Solution(object):
        def subsetsWithDup(self, nums):
            :type nums: List[int]
            :rtype: List[List[int]]
            res = [[]]
            for x, k in collections.Counter(nums).items():
                res.extend([subset + [x] * n for subset in res for n in range(1, k+1)])
            return res

Log in to reply

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