78. Subsets
Medium
Problem:
Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]Solution:
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
result = []
def dfs(index, cur):
# Append the current subset to the result
result.append(cur[:])
print(result)
# Explore further subsets by including numbers from the current index onwards
for i in range(index, len(nums)):
cur.append(nums[i])
dfs(i + 1, cur)
cur.pop()
dfs(0, [])
return resultBetter approach
Last updated