2012-01-28 3 views

答えて

14

これについて何:

numbers.scanLeft(0)((acc, n) => math.max(0, acc + n)).max 
+5

'xs.tail.scanLeft((acc、x)=>(acc + x).max(x))。 :D – lcn

6

私はスキャンソリューションに折りたたみソリューションを好む - 後者に優雅は確かにありますけれども。とにかく、

numbers.foldLeft(0 -> 0) { 
    case ((maxUpToHere, maxSoFar), n) => 
    val maxEndingHere = 0 max maxUpToHere + n 
    maxEndingHere -> (maxEndingHere max maxSoFar) 
}._2