python - subsets with dups


  • 0
    S
    class Solution(object):
        def do_subsets(self, nums, index, prefix, answer):
            
            answer.add(tuple(prefix))
            
            for i, idx in enumerate(index):
                self.do_subsets(nums, index[i+1:], prefix + [nums[idx]], answer)
            
            return answer
    
        def subsetsWithDup(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            # sort to avoid adding same subset more than once
            ret = self.do_subsets(sorted(nums), range(len(nums)), [], set())
            
            answer = []
            for t in ret:
                answer.append(list(t))
            
            return answer
            
    

Log in to reply
 

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