版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412901
思路
代码语言:javascript复制循环一次,记录当前的最大值,当前最大值小于0的时候,重新开始
code
代码语言:javascript复制func maxSubArray(nums []int) int {
cur := nums[0]
sum := nums[0]
for i := 1; i < len(nums); i {
if sum >= 0 {
sum = nums[i]
} else {
sum = nums[i]
}
if cur < sum {
cur = sum
}
}
return cur
}