6 lines Python using Set

  • 1
    class Solution(object):
        def subsetsWithDup(self, nums):
            :type nums: List[int]
            :rtype: List[List[int]]
            if not nums: return [[]]
            return list(self.helper(sorted(nums), 0))
        def helper(self, nums, idx):
            if idx == len(nums)-1: return set([(nums[-1], ), ()])
            combLis = self.helper(nums, idx+1)
            return combLis | set([(nums[idx],)+c for c in combLis])

    The iterative way is a little bit hard for me to think of, so just used a set union to eliminate duplications.

Log in to reply

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