LeetCode 78 Subsets
概述
https://leetcode.com/problems/subsets/
回溯法
class Solution {
public:
vector<vector<int>> ans;
vector<vector<int>> subsets(vector<int>& nums) {
vector<int> s;
backtrack(nums, s, 0);
return ans;
}
void backtrack(vector<int>& nums, vector<int>& s, int i) {
if (i == nums.size()) {
ans.push_back(s);
return;
}
backtrack(nums, s, i + 1);
s.push_back(nums[i]);
backtrack(nums, s, i + 1);
s.pop_back();
}
};
Links: leetcode-78-subsets