This series of articles covers a set of popular computer science problems.
The power set refers to the collection of all subsets, including the empty set and the original set itself. For an array of n unique elements, the power set contains a whopping 2^n subsets! So, for an array of just 5 numbers, you're dealing with 32 unique combinations waiting to be discovered.
Given an integer array nums
of unique elements, we need to return all possible subsets
The solution must not contain duplicate subsets.
Solution:
def subsets(nums):
res = [[]]
for n in nums:
for i in range(len(res)):
r = res[i]
res.append(list(r) + [n])
return res
Your email address will not be published. Required fields are marked *