python - subset of distinct nums


  • 0
    S
    class Solution(object):
        def do_subsets(self, nums, prefix, answer):
            
            answer.append(prefix)
            
            for i,num in enumerate(nums):
                self.do_subsets(nums[i+1:] , prefix + [num], answer)
            
            return answer
        
        def subsets(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            
            # sort to avoid adding same subset more than once
            return self.do_subsets(sorted(nums), [], [])
    

Log in to reply
 

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