LeetCode 53 JS

pimkle發表於2019-02-24
/**
 * @param {number[]} nums
 * @return {number}
 */
var maxSubArray = function(nums) {
    let len = nums.length;
    let max = Number.MIN_SAFE_INTEGER;
    let sum = 0;
    for(let i = 0; i < len; ++i) {
        if(sum + nums[i] >= nums[i]) {
            sum += nums[i];
        } else {
            sum = nums[i];
        }
        if(sum > max) {
            max = sum;
        }
    }
    return max;
};
複製程式碼

相關文章