53. 最大子序和

2020-03-18  本文已影响0人  寂灭天骄小童鞋

https://leetcode-cn.com/problems/maximum-subarray/

func maxSubArray(_ nums: [Int]) -> Int {
    var curMax = nums[0]
    var sum = nums[0]
    for idx in stride(from: 1, to: nums.count, by: 1) {
        curMax = max(nums[idx], curMax + nums[idx])
        sum = max(curMax, sum)
    }
    return sum
}

上一篇 下一篇

猜你喜欢

热点阅读