【发布时间】:2019-02-21 14:15:48
【问题描述】:
我的代码没问题,但是在一些测试用例上总是抽空,有什么改进的方法吗?我的猜测是 indexOf 函数耗时太长。
func checkMagazine(magazine: [String], note: [String]) -> Void {
var mutableMag = magazine
if note.count > mutableMag.count {
print("No")
return
}
for word in note {
if let index = mutableMag.index(of: word) {
mutableMag.remove(at: index)
} else {
print("No")
return
}
}
print("Yes") }
请在此链接中找到挑战:https://www.hackerrank.com/challenges/ctci-ransom-note/problem
【问题讨论】:
-
您已经得到了很好的答案,但请注意,这也是codereview.stackexchange.com 上的一个好问题。他们有专门的标签“programming-challenge”和“time-limit-exceeded”来解决此类问题。
标签: arrays swift dictionary swift3 hashmap