思路:
dfs
代码:
class Solution {
public:
vector<vector<int>>res;
vector<int> path;
int n;
vector<vector<int>> subsets(vector<int>& nums) {
n=nums.size();
dfs(nums,0);
return res;
}
void dfs(vector<int>& nums,int u){
if(u <= n){
res.push_back(path);
}
if(u == n) return;
//搜索顺序是nums里面的元素
for(int i=u;i<n;i++){
path.push_back(nums[i]);
dfs(nums,i+1);
path.pop_back();
}
}
};
评论区