题解 | #连续子数组的最大和#
连续子数组的最大和
http://www.nowcoder.com/practice/459bd355da1549fa8a49e350bf3df484
package main
/**
*
* @param array int整型一维数组
* @return int整型
*/
func FindGreatestSumOfSubArray( array []int ) int {
// write code here
max1, max2 := 0, 0
for i, v := range array {
if i == 0 {
max1, max2 = v, v
continue
}
if max1 < max2 {
max1 = max2
}
if max2 + v > v {
max2 = max2 + v
} else {
max2 = v
}
}
if max1 > max2 {
return max1
}
return max2
}