#78 Subsets ——Top 100 Liked Questions
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
“””
第一次:迭代外层循环遍历nums,内层循环遍历结果集res
“””class Solution(object):
def subsets(self, nums):
“””
:type nums: List[int]
List[List[int]]
“””
res = [[]]
for num in nums:
for item in res:
res = res + [item + [num]]
return res“””
Runtime: 24 ms, faster than 91.49% of Python online submissions for Subsets.
Memory Usage: 11.8 MB, less than 79.42% of Python online submissions forSubsets.
“””
还没有评论,来说两句吧...