- 空集也是子集
- 算法的神奇之处在于收集结果的代码的位置
#include <iostream>
#include <vector>
class Solution {
private:
std::vector<int> path;
std::vector<std::vector<int>> result;
void backtracking(std::vector<int>& nums, int startIndex) {
result.push_back(path);
if (startIndex >= nums.size())
return;
for (int i = startIndex; i < nums.size(); ++i) {
path.push_back(nums.at(i));
backtracking(nums, i + 1);
path.pop_back();
}
}
public:
std::vector<std::vector<int>> subsets(std::vector<int>& nums) {
backtracking(nums, 0);
return result;
}
};
int main()
{
Solution s;
return 0;
}