本文主要是介绍#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]
:rtype: 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.
"""
这篇关于#78 Subsets ——Top 100 Liked Questions的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!