78. Subsets
· 약 1분
[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](https://leetcode.com/problems/subsets/)
1. 내장 함수 사용
from itertools import combinations
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
answer=[]
for length in range(len(nums)+1):
for item in combinations(nums, length):
answer.append(list(item))
return answer

2. 직접 구현
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
answer = []
def comb(cnt, cur_index, result):
if cur_index == len(nums):
answer.append(result)
return
comb(cnt + 1, cur_index + 1, result + [nums[cur_index]])
comb(cnt, cur_index + 1, result)
comb(0, 0, [])
return answer

오랜만에 직접 구현해봤는데 이쁘게 해보려고 하다가 삽질을 너무 많이 했다.