78. Subsets (Bit manipulation)

class Solution(object): def subsets(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ n = len(nums) res = [] for i in range(0, 2**n): # every int represents one combination # now only need to translate this representation to a list elements = [] for pos in range(0, n): mask = 1 << pos if (mask & i): elements.append(nums[pos]) res.append(elements) return res
Bit manipulation

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.