题解 | #寻找两头牛的情报关联#
寻找两头牛的情报关联
https://www.nowcoder.com/practice/5cb34321b6dd41bcbea3db454c2f6e9f
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param info int整型vector * @param target_weight int整型 * @return int整型vector */ vector<vector<int>> result; vector<int> path; void backTracking(vector<int>& info,int startIndex,int target_weight,int sumNum) { if (sumNum == target_weight && path.size() == 2) { result.emplace_back(path); return; } for (int i = startIndex;i < info.size();++i) { sumNum += info[i]; path.push_back(i + 1); backTracking(info, i + 1, target_weight, sumNum); sumNum -= info[i]; path.pop_back(); } } vector<int> findWeightRelation(vector<int>& info, int target_weight) { // write code here backTracking(info, 0, target_weight, 0); if (result.size() == 0) { return {-1,-1}; } return result[0]; } };
我只能想到回溯