python bit manipulation


  • 0
    G
    import math
    class Solution(object):
        def helper(self,res,nums,i):
            tmp=[]
            for j in range(len(nums)):
                if i&1:
                    tmp.append(nums[j])
                i>>=1
            res.append(tmp)
        def subsets(self,nums):
            if not nums:
                return [[]]
            res,state=[],int(math.pow(2,len(nums)))
            for i in range(state):
                self.helper(res,nums,i)
            return res
    

Log in to reply
 

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