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
풀이 과정
주어진 배열의 부분 집합을 구하는 문제이다.
재귀로 배열의 각 인덱스에 해당하는 원소가 부분 집합에 들어갈지, 안들어갈지 판단해주면서 answer 배열에 추가해주었다. 어떤 배열이 주어지던, 부분 집합에 공집합은 반드시 포함되므로, 정답 배열에 공집합을 넣어놓고 재귀 함수를 수행하였다.
소스 코드
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
answer = [[]]
output = []
def subset(index):
if index >= len(nums):
return
output.append(nums[index])
answer.append(output[:])
subset(index+1)
output.pop()
subset(index+1)
subset(0)
return answer
'알고리즘 문제 풀이 > 리트코드' 카테고리의 다른 글
LeetCode - 207. Course Schedule [Python] (0) | 2022.08.25 |
---|---|
LeetCode - 332. Reconstruct Itinerary [Python] (0) | 2022.08.21 |
LeetCode - 39. Combination Sum [Python] (0) | 2022.08.12 |
LeetCode - 77. Combinations [Python] (0) | 2022.08.12 |
LeetCode - 46. Permutations [Python] (0) | 2022.08.11 |