classSolution:defsubsets(self, nums: List[int])-> List[List[int]]:
a =[]for i inrange(len(nums)+1):for i in itertools.combinations(nums,i):
a.append(list(i))return a
# 执行用时 :56 ms, 在所有 Python3 提交中击败了84.55%的用户# 内存消耗 :13 MB, 在所有 Python3 提交中击败了96.40%的用户
算法说明: 用内置的函数《itertools》进行排列组合,转换为列表添加到列表a中输出!
代码2:
classSolution:defsubsets(self, nums: List[int])-> List[List[int]]:
s =[[]]for i in nums:
s = s +[[i]+ j for j in s]return s
# 执行用时 :52 ms, 在所有 Python3 提交中击败了93.47%的用户# 内存消耗 :13.1 MB, 在所有 Python3 提交中击败了84.21%的用户