- #1
Andrew1235
- 5
- 1
The following Python 3 code is provided as the solution to this problem (https://leetcode.com/problems/subsets/solution/) that asks to find all subsets of a list of integers. For example, for the list below the output is [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]].
I am not familiar with backtracking. Can someone explain how the code works?
alist = [1,2,3]
ans = []
def backtrack(nums, start, curr):
ans.append(curr)
for i in range(start, len(nums)):
backtrack(nums, i+1, curr + [nums])
def subsets(nums):
backtrack(nums, 0, [])
return ans
print(subsets(alist))
I am not familiar with backtracking. Can someone explain how the code works?
alist = [1,2,3]
ans = []
def backtrack(nums, start, curr):
ans.append(curr)
for i in range(start, len(nums)):
backtrack(nums, i+1, curr + [nums])
def subsets(nums):
backtrack(nums, 0, [])
return ans
print(subsets(alist))