【发布时间】:2017-05-19 08:12:38
【问题描述】:
我正在尝试使用 Scala 解决 Codility 的 GenomicRangeQuery 问题,为此我编写了以下函数:
def solution(s: String, p: Array[Int], q: Array[Int]): Array[Int] = {
for (i <- p.indices) yield {
val gen = s.substring(p(i), q(i) + 1)
if (gen.contains('A')) 1
else if (gen.contains('C')) 2
else if (gen.contains('G')) 3
else if (gen.contains('T')) 4
}
}
我没有做很多测试,但似乎可以解决问题。
我的问题是 for 理解返回 scala.collection.immutable.IndexedSeq[AnyVal],而函数必须返回 Array[Int],因此它抛出了 type mismatch error。
有没有办法让 for 理解返回 Array[Int] 或将 IndexedSeq[AnyVal] 转换为 Array[Int]?
【问题讨论】: