如何优化 Dictionary 和 while 循环
How to do optimization of the Dictionary and while loop
我正在 hackerrank 网站上解决任务并收到有趣的 Runtime Error
,它与测试中的数据量有关。
任务:
Given names and phone numbers, assemble a phone book that maps
friends' names to their respective phone numbers. You will then be
given an unknown number of names to query your phone book for. For
each queried, print the associated entry from your phone book on a
new line in the form name=phoneNumber; if an entry for is not found,
print Not found instead.
我的解决方案:
let n = Int(readLine()!)! //Amount of tests
let count = n * 2//Insertion to the dictionary and validation of the data
var step = 0
var book = Dictionary<String, String>(minimumCapacity: n)
while step < count {
if (step < n ) {
let bookData = readLine()!.characters.split{[=10=] == " "}.map(String.init)
book[bookData[0]] = bookData[1]
} else {
let name = readLine()!
if let num = book[name] as String! {
print("\(name)=\(num)")
} else {
print("Not found")
}
}
step += 1
}
我有兴趣优化代码以避免 Runtime Error
。我已经做了一些调整,它们与 Dictionary
的 minimumCapacity
相关联,并且使用 while
而不是 for-loop
。你能告诉我应该修复什么吗?
输入数据包含固定部分(条目数N
,
后跟 N
行 name/number 对)和 可变部分
(查询)。
您可以对固定部分使用强制展开 readLine()!
因为
你知道它会成功。但是对于可变部分你有
调用 readLine()
直到 returns nil
.
代码将如下所示:
let n = Int(readLine()!)! // Number of entries
var book = [String: String](minimumCapacity: n)
for _ in 1...n {
let bookData = readLine()!.characters.split{[=10=] == " "}.map(String.init)
book[bookData[0]] = bookData[1]
}
while let name = readLine() {
if let num = book[name] {
print("\(name)=\(num)")
} else {
print("Not found")
}
}
我正在 hackerrank 网站上解决任务并收到有趣的 Runtime Error
,它与测试中的数据量有关。
任务:
Given names and phone numbers, assemble a phone book that maps friends' names to their respective phone numbers. You will then be given an unknown number of names to query your phone book for. For each queried, print the associated entry from your phone book on a new line in the form name=phoneNumber; if an entry for is not found, print Not found instead.
我的解决方案:
let n = Int(readLine()!)! //Amount of tests
let count = n * 2//Insertion to the dictionary and validation of the data
var step = 0
var book = Dictionary<String, String>(minimumCapacity: n)
while step < count {
if (step < n ) {
let bookData = readLine()!.characters.split{[=10=] == " "}.map(String.init)
book[bookData[0]] = bookData[1]
} else {
let name = readLine()!
if let num = book[name] as String! {
print("\(name)=\(num)")
} else {
print("Not found")
}
}
step += 1
}
我有兴趣优化代码以避免 Runtime Error
。我已经做了一些调整,它们与 Dictionary
的 minimumCapacity
相关联,并且使用 while
而不是 for-loop
。你能告诉我应该修复什么吗?
输入数据包含固定部分(条目数N
,
后跟 N
行 name/number 对)和 可变部分
(查询)。
您可以对固定部分使用强制展开 readLine()!
因为
你知道它会成功。但是对于可变部分你有
调用 readLine()
直到 returns nil
.
代码将如下所示:
let n = Int(readLine()!)! // Number of entries
var book = [String: String](minimumCapacity: n)
for _ in 1...n {
let bookData = readLine()!.characters.split{[=10=] == " "}.map(String.init)
book[bookData[0]] = bookData[1]
}
while let name = readLine() {
if let num = book[name] {
print("\(name)=\(num)")
} else {
print("Not found")
}
}