最大子数组和

给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

子数组 是数组中的一个连续部分。

1
2
3
4
5
6
7
8
9
10
const maxSubArray = (nums = [-2,1,-3,4,-1,2,1,-5,4]) => {
    const length = nums.length
    if(length === 1) return nums[0]
    let pre = nums[0], max = nums[0];
    for(let i = 1; i < length;i++){
        pre = Math.max(pre + nums[i], nums[i]);
        max = Math.max(max, pre);
    }
    return max
};

  Leecode 提交通过

posted @   671_MrSix  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示