Very concise Python recursive divide-and-conquer solution


  • 0
    C

    Divide the original sets into two smaller sets, get the sub sets of each, then combine the subsets.

        def subsets(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            if len(nums) == 1:
                return [nums, []]
    
            # Divide the the set into two smaller sets
            l = len(nums) // 2
            set_a = self.subsets(nums[:l])
            set_b = self.subsets(nums[l:])
            set_all = [a+b for a in set_a for b in set_b]
            return set_all
    

Log in to reply
 

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