一. 话不多说,参考前面大神思路模板.
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> res;
void backtrace(int start, vector<int> tmp, vector<int>& nums) {
//关键是每次进来一个新值之前,都将当前tmp添加到res.
res.push_back(tmp);
//如果遍历到终止位置,直接return.
if (start == nums.size())
return;
for (int i = start; i <= nums.size() - 1; i++) {
tmp.push_back(nums[i]);
backtrace(i + 1, tmp, nums);
tmp.pop_back();
}
}
vector<vector<int>> subsets(vector<int>& nums) {
vector<int> tmp;
backtrace(0, tmp, nums);
return res;
}
};