输入：nums = [-2,1,-3,4,-1,2,1,-5,4]输出：6解释：连续子数组 [4,-1,2,1] 的和最大，为 6 。

## 解法

1. 暴力解法

/** * @param {number[]} nums * @return {number} */var maxSubArray = function (nums) {  let max = -Infinity;  for (let i = 0; i < nums.length; i++) {    let sum = 0;    for (let j = i; j < nums.length; j++) {      sum = sum + nums[j];      max = Math.max(max, sum);    }  }  return max;};

2. 动态规划

if dp[i - 1] > 0 : dp[i] = dp[i - 1] + nums[i]
else : dp[i] = nums[i]

/** * @param {number[]} nums * @return {number} */var maxSubArray = function (nums) {    // dp[i]: dp[i] 表示第 i 个数结尾的连续子数组的最大和    let dp = [];    let max = -Infinity;    for (let i = 0; i < nums.length; i++) {        if (dp[i - 1] > 0) {            dp[i] = dp[i - 1] + nums[i]        } else {            dp[i] = nums[i]        }        max = Math.max(max, dp[i])    }    return max};

/** * @param {number[]} nums * @return {number} */var maxSubArray = function (nums) {    let max = nums[0];    let pre = 0;    for (let i = 0; i < nums.length; i++) {        pre = Math.max(pre + nums[i], nums[i]);        max = Math.max(max, pre);    }    return max;};