class Solution {
private:
vector<int> candidates;
vector<vector<int>> vec_vec_int;
vector<int> vec_int;
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(),candidates.end());
this->candidates = candidates;
backtrack(target, 0);
return vec_vec_int;
}
void backtrack(int target, int start){
if(target == 0){
vec_vec_int.push_back(vec_int);
return;
}
for(int i = start; i < candidates.size() && target - candidates[i] >= 0; i++){ //剪枝
if(i > start && candidates[i] == candidates[i-1])
continue;
vec_int.push_back(candidates[i]);
backtrack(target - candidates[i], i + 1);
vec_int.pop_back();
}
}
};
Accepted
175/175 cases passed (8 ms)
Your runtime beats 55.79 % of cpp submissions
Your memory usage beats 94.06 % of cpp submissions (10.3 MB)