题解 | #农场牛类别匹配#
农场牛类别匹配
https://www.nowcoder.com/practice/270db1e1d65b4366a49a517ec7822912
#include <endian.h> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param breeds int整型vector * @param target_sum int整型 * @return int整型 */ int result = 0; vector<int> path; void backTracking(vector<int>& breeds,int startIndex,int target_sum) { if (path.size() == 2 && path[0] + path[1] == target_sum) { result++; return; } for (int i = startIndex;i < breeds.size();++i) { if (path.size() == 1) { if (path[0] + breeds[i] > target_sum || breeds[i] >= target_sum) { continue; } } path.push_back(breeds[i]); backTracking(breeds, i + 1, target_sum); path.pop_back(); } } int countMatchingPairs(vector<int>& breeds, int target_sum) { // write code here backTracking(breeds,0,target_sum); return result; } };
回溯!??