[53] 最大子数组和

34 阅读1分钟
/*
 * @lc app=leetcode.cn id=53 lang=javascript
 *
 * [53] 最大子数组和
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number}
 */
var maxSubArray = function (nums) {
  // 初始化第一个为值,所以for循环从1开始
  let max = nums[0]
  for (let i = 1; i < nums.length; i++) {
    nums[i] = Math.max(nums[i] + nums[i - 1], nums[i])
    max = Math.max(max, nums[i])
  }
  return max
}
// @lc code=end