반응형
https://leetcode.com/problems/subsets/
Subsets - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Think
dfs 탐색 과정의 모든 걸 저장하면 됨
Solution
class Solution(object):
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
def dfs(index, path) :
# 매번 결과에 추가함. 그래프 끝까지 갔을 때 따로 처리해줄 필요 없음
result.append(path)
for i in range(index, len(nums)) :
dfs(i+1, path + [nums[i]])
# dfs(i+1, path.append(nums[i])) # 이렇게 쓰면 에러남. 초기값인 빈 배열에는 append가 안됨..!
result = []
dfs(0, [])
return result
Get
빈 배열에는 append가 안됨..!
append대신 더할 값을 []로 한 번 감싸서 배열로 만 든 후 배열 + 배열 형태로 +연산자를 사용할 것
반응형
'DSA > Algorithm' 카테고리의 다른 글
[LeetCode 110] Balanced Binary Tree (0) | 2022.10.11 |
---|---|
[LeetCode 104] Maximum Depth of Binary Tree (0) | 2022.10.10 |
[LeetCode 77] Combinations (0) | 2022.10.04 |
[LeetCode 17] Letter Combinations of a Phone Number (1) | 2022.10.04 |
[LeetCode 771] Jewels and Stones (0) | 2022.09.27 |