Leetcode Golang 53. Maximum Subarray.go

2019-04-12 11:22:00 浏览数 (1)

版权声明:原创勿转 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
}

0 人点赞