78. Subsets
- Subsets python solution
题目描述
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
解析
题目中有条很关键的约束就是distinct,保证数组里各个元素都不同,这样就不用担心重复的情况,大大降低难度。
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
result=[[]]
for num in nums:
result+=[i+[num] for i in result]
return result
Reference
https://leetcode.com/problems/subsets/discuss/27356/5-lines-of-python