Given an integer array nums, return all possible subsets (the power set).
The solution set must not contain duplicate subsets.
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
if len(nums) == 1:
return [[],[nums[0]]]
def backtracking(nums, path):
if not nums:
result.append(path)
for i in range(0, len(nums)):
backtracking(nums[i+1:], path+[nums[i]])
result = [[]]
num = []
for i in range(0, len(nums)):
num.append(nums[i])
backtracking(num, path = [])
return result
permutations 트라우마가 24 시간도 안돼서 부활하다...
보자마자 backtracking 을 생각했다
permutations 의 backtracking
def backtracking(nums, path): if not nums: result.append(path) for i in range(0, len(nums)): backtracking(nums[:i] + nums[i+1:], path+[nums[i]] ) result = [] backtracking(nums, path = [])
backtracking 은 무조건 외우는 걸로...^^