78_Subsets
Last updated
Last updated
def subsets(nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
def helper(i):
# i is the position of integer we are handling
if i == len(nums):
output.append(list(sol))
return
# case when nums[i] is not in the subset
helper(i + 1)
# case when nums[i] is in the subset
sol.append(nums[i])
helper(i + 1)
sol.pop()
output, sol = [], []
helper(0)
return outputdef subsets(nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
def helper(to_be_selected, selected_so_far):
# to_be_selected is the position of integer we are handling
if to_be_selected == len(nums):
output.append(list(selected_so_far))
return
# case when nums[to_be_selected] is not in the subset
helper(to_be_selected + 1, selected_so_far)
# case when nums[to_be_selected] is in the subset
helper(to_be_selected + 1, selected_so_far + [nums[to_be_selected]])
output = []
helper(0, [])
return output