문제

리트코드 78. Subsets
난이도: ⭐⭐

Given an integer array nums of unique elements, return all possible subsets (the power set).

The solution set must not contain duplicate subsets. Return the solution in any order.
(모든 부분 집합을 리턴하라)

예시 1

Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

예시 2

Input: nums = [0]
Output: [[],[0]]

풀이 1: 트리의 모든 DFS결과

class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        result = []
        
        def dfs(index, path):
            result.append(path)
            
            for i in range(index, len(nums)):
                dfs(i+1, path+[nums[i]])
        
        dfs(0, [])

        return result

Tags:

Categories:

Updated: