1 Star 0 Fork 332

[]~( ̄ ̄)~* / leetcode

forked from doocs / leetcode 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README_EN.md 1.09 KB
一键复制 编辑 原始数据 按行查看 历史
ylb 提交于 2021-12-24 22:51 . style: format code and docs (#645)

16.17. Contiguous Sequence

中文文档

Description

You are given an array of integers (both positive and negative). Find the contiguous sequence with the largest sum. Return the sum.

Example:


Input:  [-2,1,-3,4,-1,2,1,-5,4]

Output:  6

Explanation:  [4,-1,2,1] has the largest sum 6.

Follow Up:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

Solutions

Python3

Java

JavaScript

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

...

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/keshuimao/leetcode.git
git@gitee.com:keshuimao/leetcode.git
keshuimao
leetcode
leetcode
main

搜索帮助

344bd9b3 5694891 D2dac590 5694891