aboutsummaryrefslogtreecommitdiff
path: root/subsets.py
blob: 1897721a78e236b8fdcb4964812b399c1285e0b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 78. Subsets

"""
given an integer array 'nums' of unique elements return all possible subsets
(the power set). the solution set must not contain duplicate subsets. return
the solution in any order.
"""


class Solution(object):
    def subsets(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        ans = []
        nums.sort()
        for i in range(1 << len(nums)):
            tmp = []
            for j in range(len(nums)):
                if i & 1 << j:
                    tmp.append(nums[j])
            ans.append(tmp)
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(obj.subsets(nums=[1, 2, 3]))
    print(obj.subsets(nums=[0]))