感觉和39题一波一样的思想。。。。。easy
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
if(candidates.size() < 0)
return {};
sort(candidates.begin(), candidates.end());
if(target < candidates[0])
return {};
vector<vector<int> > result;
vector<int> temp;
int sum = 0;
getBacking(result, temp, candidates, sum, 0, target);
sort(result.begin(), result.end());
auto ite = unique(result.begin(), result.end());
result.erase(ite,result.end());
return result;
}
bool getBacking(vector<vector<int> >& result,vector<int>& temp,vector<int> nums, int sum, int k, int target){
if(sum > target){
return false;
}else if(sum == target){
result.push_back(temp);
return false;
}else{
for(int i=k;i<nums.size();i++){
bool flag = true;
temp.push_back(nums[i]);
sum += nums[i];
if(i != nums.size()-1){
flag = getBacking(result, temp, nums, sum, i+1, target);
}
if(i == nums.size()-1 && sum == target){
result.push_back(temp);
}
sum -= nums[i];
temp.pop_back();
if(!flag)
break;
}
return true;
}
}
};
int main(){
return 0;
}