题解 | #农场牛类别匹配#
农场牛类别匹配
https://www.nowcoder.com/practice/270db1e1d65b4366a49a517ec7822912
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param breeds int整型一维数组
* @param target_sum int整型
* @return int整型
*/
function countMatchingPairs(breeds, target_sum) {
// write code here
let map = new Map()
for (let i = 0; i < breeds.length; i++) {
if (target_sum - breeds[i] != breeds[i]) {
map.set(breeds[i], target_sum - breeds[i])
}
}
let res = []
for (const b of breeds) {
let value = map.get(b)
if (map.has(value)) {
res.push([b, value])
map.delete(value)
}
}
return res.length
}
module.exports = {
countMatchingPairs: countMatchingPairs
};

查看5道真题和解析