LeetCode 78 Subsets (Python)

Posted by 小明MaxMing on July 11, 2020

题目

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

Note: The solution set must not contain duplicate subsets.

解题思路

从空开始,每一步都向子集添加新的整数,并生成新的子集

代码

class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        res = [[]]
        for num in nums:
            res += [item+[num] for item in res]
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅