题解 | #合并表记录#

合并表记录

https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201

import Foundation

while let line = readLine() {
    var count = Int(line)!

    var map = [Int: Int]()
    while count > 0 {
        let row = readLine()
        let array = row!.split(separator: " ")
        let key = Int(array[0])!
        var value = Int(array[1])!
        if let v = map[key] {
            value += v
        }
        map[key] = value
        count -= 1
    }

    let keys = map.keys.sorted(by: { $0 < $1 })
    for key in keys {
        print("\(key) \(map[key]!)")
    }
}

全部评论

相关推荐

08-19 19:57
石河子大学 C++
企鹅百度字节的孝子:为啥本科只有两年啊
校招求职吐槽
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务