题解 | #牛群喂食#
牛群喂食
https://www.nowcoder.com/practice/591c222d73914c1ba031a660db2ef73f
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param candidates int整型vector * @param target int整型 * @return int整型vector<vector<>> */ vector<vector<int> >ve; vector<int>pa; void dfs(vector<int>& candidates, int target) { if (target < 0) return; if (target == 0) { int s[51] = {0}; for (int i = 0; i < pa.size(); i++) s[pa[i]]++; int add = 0; for (int i = 0; i < ve.size(); i++) { int temp[51] = {0}; for (int j = 0; j < ve[i].size(); j++) { temp[ve[i][j]]++; } int flag = 0; for (int k = 0; k <= 50; k++) { if (temp[k] != s[k]) { flag = 1; break; } } if(!flag) { add = 1; break; } } if (!add) { ve.push_back(pa); return; } } for (int i = 0; i < candidates.size(); i++) { pa.push_back(candidates[i]); target -= candidates[i]; dfs(candidates, target); target += pa.back(); pa.pop_back(); } } vector<vector<int> > cowCombinationSum(vector<int>& candidates, int target) { dfs(candidates, target); return ve; } };