题目描述
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
样例
Example:
Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
算法1
(dfs) $O(2^n)$
dfs,注意在往下搜索过程中,i变成i+1
C++ 代码
class Solution {
public:
vector<vector<int>> res;
vector<vector<int>> subsets(vector<int>& nums) {
vector<int> vec;
dfs(nums,0,vec);
return res;
}
void dfs(vector<int>&nums,int x,vector<int>&vec){
res.push_back(vec);//每一个都是一个集合
for(int i=x;i<nums.size();i++){
vec.push_back(nums[i]);
dfs(nums,i+1,vec);
vec.pop_back();
}
}
};
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度分析:blablabla
C++ 代码
blablabla
复杂度应该是o(n2^n)
这个没递归出口不会死循环吗
for结束了自然就结束了啊,哪里会死循环