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
오랜만에 직접 구현해봤는데 이쁘게 해보려고 하다가 삽질을 너무 많이 했다.
'Algorithm' 카테고리의 다른 글
230. Kth Smallest Element in a BST (0) | 2021.05.07 |
---|---|
739. Daily Temperatures (0) | 2021.05.06 |
1. Two Sum (0) | 2021.05.05 |
206. Reverse Linked List (0) | 2021.05.05 |
234. Palindrome Linked List (0) | 2021.05.04 |