Implement subsets(nums), returning every subset as a list of lists.
Use the standard include/exclude backtracking: at each node append a copy of the current path, then loop over indices from a start pointer and recurse with i + 1. This produces a fixed, deterministic order.
For [1, 2] the output is [[], [1], [1, 2], [2]]. For [1, 2, 3] it is [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]].
def subsets(nums):
"""Return every subset of nums as a list of lists.
Explore include/exclude by index. At each recursion node append a copy
of the current path, then extend using indices from start onward.
"""
# TODO: implement backtracking that records a copy of path at each node
# and recurses with i + 1 to keep index order.
passThe editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
Implement subsets(nums), returning every subset as a list of lists.
Use the standard include/exclude backtracking: at each node append a copy of the current path, then loop over indices from a start pointer and recurse with i + 1. This produces a fixed, deterministic order.
For [1, 2] the output is [[], [1], [1, 2], [2]]. For [1, 2, 3] it is [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]].
Press Run Tests or ββ© to check your solution.