Easy to understand Python with Bit manipulation


  • 0
    S
    class Solution(object):
        def subsets(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            size = 2 ** len(nums)
            result = []
            for i in range(size):
                index = 0
                cur = i
                cur_set = []
                while cur > 0:
                    if cur & 1 == 1:
                        cur_set.append(nums[index])
                    cur >>= 1
                    index += 1
                result.append(cur_set)
            return result
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.