#78 Subsets ——Top 100 Liked Questions

本是古典 何须时尚 2022-01-27 02:09 333阅读 0赞

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

  1. Input: nums = [1,2,3]
  2. Output:
  3. [
  4. [3],
  5. [1],
  6. [2],
  7. [1,2,3],
  8. [1,3],
  9. [2,3],
  10. [1,2],
  11. []
  12. ]

“””

第一次:迭代外层循环遍历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.

“””

发表评论

表情:
评论列表 (有 0 条评论,333人围观)

还没有评论,来说两句吧...

相关阅读