Skip to Content

Find the powerset of a set

Home | Coding Interviews | Arrays and Strings | Find the powerset of a set

Given a set of distinct integers, return the power set (all possible subsets including the empty set)

There are a few ways to approach finding the powerset.

class Solution(object):
    def subsets(self, nums):
        ret = []
        self.dfs(nums, [], ret)
        return ret
    
    def dfs(self, nums, path, ret):
        ret.append(path)
        for i in range(len(nums)):
            self.dfs(nums[i+1:], path+[nums[i]], ret)
       
    # Bit Manipulation    
    def subsets2(self, nums):
        res = []
        nums.sort()
        for i in xrange(1<<len(nums)):
            tmp = []
            for j in xrange(len(nums)):
                if i & 1 << j:  # if i >> j & 1:
                    tmp.append(nums[j])
            res.append(tmp)
        return res
		
    # Iteratively
    def subsets(self, nums):
        res = [[]]
        for num in sorted(nums):
            res += [item+[num] for item in res]
        return res

Iteratively

Backtracking/dfs:

Bit manipulation:

Posted by Jamie Meyer 20 days ago