Python Recursive, only 4 lines (beats 98%)


  • 0
    G

    Very straightforward solution using recursion.

    class Solution(object):
        def subsets(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            
            if not nums:
                return [[]]
            prevSubsets = self.subsets(nums[0:len(nums) - 1])
            return prevSubsets + [subset + [nums[-1]] for subset in prevSubsets]
    

Log in to reply
 

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