Home Technology The Power Set: All Subsets of a Given Set
Information Technology

The Power Set: All Subsets of a Given Set

by admin - 2024/02/14
IMG

 

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

Comments



Leave a Comment

Your email address will not be published. Required fields are marked *

Popular Articles