Scala 中的 Seq[String, Int] 到 Seq[Int]
Seq[String, Int] to Seq[Int] in Scala
我的问题是为给定的字符串创建后缀数组。
到目前为止,我已经将字符串的尾部与索引配对并按字符串对它们进行排序。
我需要删除元组的字符串部分,以便可以 return Seq[Int]
,但我不知道该怎么做。
这是我尝试做的:
def suffixArray(s: String): Seq[Int] = s.tails.zipWithIndex.toSeq.sortBy(_._1)
你可以简单地映射它:
seq.map(_._2)
或使用模式匹配:
seq.map { case(s, i) -> i }
尝试
def suffixArray(s: String): Seq[Int] = s.tails.zipWithIndex.toSeq.sortBy(_._1).map(_._2)
我的问题是为给定的字符串创建后缀数组。
到目前为止,我已经将字符串的尾部与索引配对并按字符串对它们进行排序。
我需要删除元组的字符串部分,以便可以 return Seq[Int]
,但我不知道该怎么做。
这是我尝试做的:
def suffixArray(s: String): Seq[Int] = s.tails.zipWithIndex.toSeq.sortBy(_._1)
你可以简单地映射它:
seq.map(_._2)
或使用模式匹配:
seq.map { case(s, i) -> i }
尝试
def suffixArray(s: String): Seq[Int] = s.tails.zipWithIndex.toSeq.sortBy(_._1).map(_._2)