golang动态规划求解最大连续子数组和

Tue Sep 5, 2017

100 Words|Read in about 1 Min
Tags: 算法  
// 求最大连续子数组和
func MaxSubArray(arr []int) int {
	currSum := 0
	maxSum := arr[0]

	for _, v := range arr {
		if currSum > 0 {
			currSum += v
		} else {
			currSum = v
		}
		if maxSum < currSum {
			maxSum = currSum
		}
	}
	return maxSum
}

See Also

Tue Sep 5, 2017

100 Words|Read in about 1 Min
Tags: 算法