- - 继续暴力把
class Solution {
void dfs(vector<pair<int, int>> &vp, int p, int target, vector<int> & stk,
vector<vector<int> > & ans){
if(target == 0){
ans.push_back(stk);
return ;
}
if(p >= vp.size())
return ;
// not use
dfs(vp, p + 1, target, stk, ans);
// use
int i = 1;
for(; i <= vp[p].second && i * vp[p].first <= target; ++ i){
stk.push_back(vp[p].first);
dfs(vp, p + 1, target - i * vp[p].first, stk, ans);
}
while(--i > 0)
stk.pop_back();
}
public:
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
map<int, int> hash;
for(int i = 0; i < num.size(); ++ i)
hash[num[i]] ++ ;
vector<pair<int, int> > vp;
for(map<int, int> :: iterator it = hash.begin();
it != hash.end(); ++ it)
vp.push_back(make_pair(it->first, it->second));
vector<vector<int> > ans;
vector<int> stk;
dfs(vp, 0, target, stk, ans);
return ans;
}
};