Created
January 18, 2011 10:28
-
-
Save songpp/784267 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package algrithsm | |
import annotation.tailrec | |
/** | |
* User: flower | |
* Date: 11-1-18 | |
* Time: 下午6:22 | |
* Description: | |
*/ | |
object Numbers { | |
import java.lang.Math.max | |
def maxSum(arr: List[Int]): Int = { | |
var maxNumber = arr(0) | |
@tailrec | |
def maxSumOfArray(numbers: List[Int], maxEnd: Int, maxSum: Int): Int = numbers match { | |
case (x :: xs) => | |
maxNumber = max(x,maxNumber) | |
val end = max(maxEnd + x, 0) | |
maxSumOfArray(xs, end, max(end, maxSum)) | |
case _ => maxSum | |
} | |
val result = maxSumOfArray(arr, 0, 0) | |
if(result == 0) | |
maxNumber | |
else | |
result | |
} | |
def main(args: Array[String]) { | |
Console println maxSum(-2 :: -1 :: -3 :: -1 :: Nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment