Leetcode # 78 (Python): Subsets

정욱·2021년 4월 18일
0

Leetcode

목록 보기
9/38

Subsets

  • Difficulty: Medium
  • Type: DFS/BFS
  • link

Problem

Solution

  • Use DFS to update possible subsets
  • Stoping condition: starting index is large or equal to the length of nums
class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        result = []
        def dfs(start,path):
            if start > len(nums):
                return
            elif start == len(nums):
                result.append(path)
                return

            result.append(path)
            for i in range(start,len(nums)):
                dfs(i+1,path+[nums[i]])

        dfs(0,[])
        return result

1개의 댓글

comment-user-thumbnail
2022년 1월 3일

I found that solution is very popular and helpful: https://youtu.be/NS01_5oZn7c

답글 달기