Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
For example, given candidate set 10,1,2,7,6,1,5
and target 8
,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
class Solution { struct Info { vector<pair<int, int>>pre;//pair<int,int>里的first代表sinnums里的index,second代表个数 int remainsize;//剩余的数字个数 int remainvalue;//剩余的值 }; vector<vector<int>>re; void choose_one(int&k, vector<Info>&candi, map<int, int>&count, vector<int>sinnums) { vector<Info>newcandi; if (k == 1) { for (int i = 0; i < candi.size(); i++)//考虑0 if (candi[i].remainvalue%candi[i].remainsize == 0) { int nxtnum = candi[i].remainvalue / candi[i].remainsize; if (nxtnum>sinnums[candi[i].pre.back().first] && count.find(nxtnum) != count.end() && count[nxtnum] >= candi[i].remainsize) { vector<int>aa; for (int j = 0; j < candi[i].pre.size(); j++) for (int h = 0; h < candi[i].pre[j].second; h++) aa.push_back(sinnums[candi[i].pre[j].first]); for (int j = 0; j < candi[i].remainsize; j++) aa.push_back(nxtnum); re.push_back(aa); } } k--; return; } else { for (int i = 0; i < candi.size(); i++) { for (int j = 1; j <= (candi[i].remainsize - k + 1); j++) { for (int h = candi[i].pre.back().first + 1; (h < sinnums.size() - k + 1); h++) { //粗略做判断,减小候选集大小 if (candi[i].remainvalue <= sinnums[h] * j + sinnums.back()* (candi[i].remainsize - j) && candi[i].remainvalue >= sinnums[h] * j + sinnums[h + 1] * (candi[i].remainsize - j) && j <= count[sinnums[h]]) { Info info; info.pre = candi[i].pre; info.pre.push_back(pair<int, int>(h, j)); info.remainsize = candi[i].remainsize - j; info.remainvalue = candi[i].remainvalue - j*sinnums[h]; newcandi.push_back(info); } } } } k--; candi = newcandi; return; } } public: vector<vector<int>> combinationSum2(vector<int>& candidates, int target) { if (candidates.empty()) return re; map<int, int>count; for (int i = 0; i < candidates.size(); i++) count[candidates[i]]++; map<int, int>::iterator it = count.end(); it--; if (target >= 0 && count.begin()->first> target || target < 0 && it->first < target) return re; vector<int>sinnums; for (it = count.begin(); it != count.end(); it++) sinnums.push_back(it->first); int maxneednum = target/sinnums.front(); int neednum; //答案里只有一种数字 for (neednum = 1; neednum <= maxneednum;neednum++) if (target%neednum == 0 && count.find(target / neednum) != count.end() && count[target / neednum] >= neednum) { vector<int>aa(neednum, target / neednum); re.push_back(aa); } for (int i = 0; i < sinnums.size(); i++) { //答案里有k种数字,2<=k<=neednum int need = target / sinnums[i]; if (need >= 2) { for (neednum = 2; neednum <= need; neednum++) for (int k = 2; k <= neednum; k++) { if (sinnums.size() - i >= k) { int jjmax = neednum - k + 1 < count[sinnums[i]] ? neednum - k + 1 : count[sinnums[i]]; for (int j = 1; j <= jjmax; j++) { Info inf; inf.pre.push_back(pair<int, int>(i, j)); inf.remainsize = neednum - j; inf.remainvalue = target - j*sinnums[i]; vector<Info>candi; candi.push_back(inf); int kk = k - 1; while (kk > 0) choose_one(kk, candi, count, sinnums); } } } } } return re; } };