题解 | #农场牛类别匹配#

农场牛类别匹配

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
};

全部评论

相关推荐

海螺很能干:每次看到这种简历都没工作我就觉得离谱
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务