题解 | #农场牛类别匹配#
农场牛类别匹配
https://www.nowcoder.com/practice/270db1e1d65b4366a49a517ec7822912
知识点
暴力
思路
观察到时间复杂度为O(n*n),所以直接暴力枚举任意两个数,判断二者之和是否为目标值即可
代码c++
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param breeds int整型vector
* @param target_sum int整型
* @return int整型
*/
int countMatchingPairs(vector<int>& breeds, int target_sum) {
// write code here
int ans=0;
for(int i=0;i<breeds.size();i++)
{
for(int j=i+1;j<breeds.size();j++)
{
if(breeds[i]+breeds[j]==target_sum)ans++;
}
}
return ans;
}
};