小编Pau*_*ius的帖子

在Scala中检索多维数组中有界元素的最大索引

我有一个多维数组:

val M = Array.ofDim[Int](V, N)
Run Code Online (Sandbox Code Playgroud)

目标是找到最大的V维度索引,其中存在有界元素0 <w0 <= W并返回索引和元素值.

目前我有这个代码片段可以工作,但想知道是否有更好,更有效的方法来做到这一点.

M.zipWithIndex.reverse.collectFirst({
  case (arr, ind) if arr.exists(a => a <= W && a > 0) => {
    arr.zipWithIndex.find(a => a._1 <= W && a._1 > 0) match {
      case Some((weight, ind2)) => (ind, ind2, weight)
    }
  }
})
Run Code Online (Sandbox Code Playgroud)

arrays indexing scala indices

4
推荐指数
1
解决办法
145
查看次数

标签 统计

arrays ×1

indexing ×1

indices ×1

scala ×1