Subsets

Problem Id: 78 Difficulty: Medium Tag: Array Tag: Backtracking Tag: Bit Manipulation


Intuition

We could use DFS to solve this problem.

Solution


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

    def _dfs(self, nums, start, previous):
        self.ans.append(previous[:])
        for i in range(start, len(nums)):
            previous.append(nums[i])
            self._dfs(nums, i + 1, previous)
            previous.pop()