pwstrick / daily

一份搜集的前端面试题目清单、面试相关以及各类学习的资料(不局限于前端)
2.38k stars 242 forks source link

最大子序和 #1050

Open pwstrick opened 4 years ago

pwstrick commented 4 years ago

53. 最大子序和

/**
 * @param {number[]} nums
 * @return {number}
 */
var maxSubArray = function(nums) {
    const len = nums.length;
    let max = Math.max.apply(undefined, nums);
    let sum;
    for(let i=0; i<len; i++) {
        sum = nums[i];
        for(let j=i+1; j<len; j++) {
            sum += nums[j];
            max = Math.max.call(undefined, sum, max);
        }
    }
    return max;
};