1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
// @Title: 最大子序和 (Maximum Subarray)
// @Author: 15816537946@163.com
// @Date: 2020-11-27 11:33:55
// @Runtime: 12 ms
// @Memory: 3.2 MB
func maxSubArray(nums []int) int {
	maxSum, lastSum := nums[0], 0
	for i := range nums {
		if lastSum > 0 {
			lastSum += nums[i]
		} else {
			lastSum = nums[i]
		}

		if lastSum > maxSum {
			maxSum = lastSum
		}
	}
	return maxSum
}