题解 | #牛群喂食#
牛群喂食
https://www.nowcoder.com/practice/591c222d73914c1ba031a660db2ef73f
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param candidates int整型vector * @param target int整型 * @return int整型vector<vector<>> */ vector<vector<int>> result; vector<int> path; void backTracking(vector<int>& candidates,int target,int sumNum) { if (sumNum == target) { result.push_back(path); return; } for (int i = 0;i < candidates.size();++i) { if (sumNum + candidates[i] > target) { continue; } path.push_back(candidates[i]); sumNum += candidates[i]; backTracking(candidates,target,sumNum); sumNum -= candidates[i]; path.pop_back(); } } vector<vector<int> > cowCombinationSum(vector<int>& candidates, int target) { // write code here backTracking(candidates, target,0); set<vector<int>> temp; for (auto it : result) { sort(it.begin(),it.end()); temp.insert(it); } result.clear(); for (auto it : temp) { result.push_back(it); } return result; } };
不太会去重