subsets
1# @leet start 2class Solution: 3 def subsets(self, nums: list[int]) -> list[list[int]]: 4 """ 5 This function generates the powerset of a given set. 6 This can be done by starting out with the empty set, and then 7 For each item in the set, adding the current number to the end 8 of each subset and then adding that copy to the end of the current list 9 10 So, this looks like: 11 12 1. [] (empty) 13 2. [], [1] (1) 14 3. [], [1], [2], [1, 2] (1, 2) 15 4. [], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3] (1, 2, 3) 16 """ 17 res = [[]] 18 19 for num in nums: 20 res.extend([curr + [num] for curr in res]) 21 22 return res 23 24 25# @leet end 26 27 28def test(): 29 assert 2 + 2 == 4
class
Solution:
3class Solution: 4 def subsets(self, nums: list[int]) -> list[list[int]]: 5 """ 6 This function generates the powerset of a given set. 7 This can be done by starting out with the empty set, and then 8 For each item in the set, adding the current number to the end 9 of each subset and then adding that copy to the end of the current list 10 11 So, this looks like: 12 13 1. [] (empty) 14 2. [], [1] (1) 15 3. [], [1], [2], [1, 2] (1, 2) 16 4. [], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3] (1, 2, 3) 17 """ 18 res = [[]] 19 20 for num in nums: 21 res.extend([curr + [num] for curr in res]) 22 23 return res
def
subsets(self, nums: list[int]) -> list[list[int]]:
4 def subsets(self, nums: list[int]) -> list[list[int]]: 5 """ 6 This function generates the powerset of a given set. 7 This can be done by starting out with the empty set, and then 8 For each item in the set, adding the current number to the end 9 of each subset and then adding that copy to the end of the current list 10 11 So, this looks like: 12 13 1. [] (empty) 14 2. [], [1] (1) 15 3. [], [1], [2], [1, 2] (1, 2) 16 4. [], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3] (1, 2, 3) 17 """ 18 res = [[]] 19 20 for num in nums: 21 res.extend([curr + [num] for curr in res]) 22 23 return res
This function generates the powerset of a given set. This can be done by starting out with the empty set, and then For each item in the set, adding the current number to the end of each subset and then adding that copy to the end of the current list
So, this looks like:
- [] (empty)
- [], [1] (1)
- [], [1], [2], [1, 2] (1, 2)
- [], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3] (1, 2, 3)
def
test():