Today I learned
2022/01/21
회고록
항해 99, 알고리즘 1주차
교재 : 파이썬 알고리즘 인터뷰
12장 그래프
Given an integer array nums of unique elements, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.
Example 1:
Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Example 2:
Input: nums = [0]
Output: [[],[0]]
Constraints:
1 <= nums.length <= 10
-10 <= nums[i] <= 10
All the numbers of nums are unique.
https://leetcode.com/problems/subsets/
def solution(inputNum):
result = []
prev_nodes = []
next_nodes = []
def dfs(nums):
for i in range(len(nums)):
next_nodes = nums[i:]
next_nodes.remove(nums[i])
prev_nodes.append(nums[i])
result.append(prev_nodes[:])
dfs(next_nodes)
prev_nodes.pop()
dfs(inputNum)
#e를 제외한 subList와 비교하기
result.append([])
return result
if __name__ == '__main__':
inputNum = [1,2,3]
result = solution(inputNum)
print('result : ' + str(result))
재귀, DFS 훈련