题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
import Foundation while let line = readLine() { var dict = [Int:Int]() for i in 0..<Int(String(line))! { let input = readLine()!.components(separatedBy:" ") // print(Int(String(input.first!))) let index = Int(String(input.first!))! // let index = Int(String(input.first!)!)! let values = Int(String(input.last!))! if let old = dict[index] { dict[index] = values + old } else { dict[index] = values } } for key in dict.keys.sorted() { print("\(key) \(dict[key]!)") } }