Skip to content

Latest commit

 

History

History
22 lines (17 loc) · 858 Bytes

README.md

File metadata and controls

22 lines (17 loc) · 858 Bytes

Subsets

Problem can be found in here!

Solution: Recursion

class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        output_list = []
        self.dfs(output_list, nums, [], 0)
        return output_list

    def dfs(self, output_list: List[List[int]], nums: List[int], current: List[int], index: int):
        output_list.append(list(current))
        for i in range(index, len(nums)):
            current.append(nums[i])
            self.dfs(output_list, nums, current, i+1)
            current.pop()

Time Complexity: O(n*2^n), Space Complexity: O(n), where n is the length of array $nums$.