非法开始申报

时间:2018-07-21 16:41:12

标签: scala

我有以下Scala代码:

object Solution {
    def main(args: Array[String]) {
        val List(n, m) = readLine().split(" ").map(_.toInt).toList;
        val knowledge: List[Set[Int]] = (0 until n).map( _ => {
            val knows: List[Char] = readLine().toList;
            (0 until m).toSet.flatMap(topic: Int => {
                knows(topic) match {
                    case '1' => Set(topic);
                    case _ => Set[Int].empty;
                } 
            });
        }).toList;
        val teams: List[Int] = knowledge.grouped(2).map{ case(x, y) => x ++ y }.map(_.size);
        val best: Int = teams.max;
        val count = teams.filter(_ == max);
        println(best + " " + count);
    }
}

关于它,我收到此错误:

Solution.scala:16: error: illegal start of declaration
                knows(topic) match {Solution.scala:

在这一行:

knows(topic) match {

我不明白怎么了。

我在match的正文中使用flatMap

您知道这段代码有什么问题吗?

1 个答案:

答案 0 :(得分:3)

具有显式类型,您将需要

(topic: Int) =>

按照匿名函数的语法

https://www.scala-lang.org/old/node/133

根据this answer,您还可以将类型移动到

(0 until m).toSet[Int].flatMap(topic => {