题目
描述
现在有一个没有重复元素的整数集合S,求S的所有子集
注意:
你给出的子集中的元素必须按升序排列
给出的解集中不能出现重复的元素
数据范围:1≤n≤5,集合中的任意元素满足 ∣val∣≤10
要求:空间复杂度 O(n!),时间复杂度 O(n!)
思路
采用深度优先遍历的方法,主要是要过滤重复元素,并且在每一次搜索层都要将结果保存。
代码
python版本:
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param S int整型一维数组
# @return int整型二维数组
#
class Solution:
def subsets(self , S: List[int]) -> List[List[int]]:
# write code here
res = [[]]
S.sort()
length=len(S)
if length==0:
return res
curr = []
def dfs(curr, store):
nonlocal res
if curr not in res:
res.append(curr)
for i in range(len(store)):
dfs(curr+[store[i]], store[i+1:])
dfs(curr, S)
return res
c++版本:
无