其他分享
首页 > 其他分享> > LeetCode_1905_连续子数组的最大和

LeetCode_1905_连续子数组的最大和

作者:互联网

题目链接

解题思路

AC代码

class Solution {
    public int maxSubArray(int[] nums) {
        int len = nums.length;
        int[] dp = new int[len];
        dp[0] = nums[0];
        int max = dp[0];
        for (int i = 1; i < len; i++) {
            dp[i] = Math.max(dp[i - 1], 0) + nums[i];
            max = Math.max(dp[i], max);
        }
        return max;
    }
}

本地测试代码

class Solution {
    public int maxSubArray(int[] nums) {
        int len = nums.length;
        int[] dp = new int[len];
        dp[0] = nums[0];
        int max = dp[0];
        for (int i = 1; i < len; i++) {
            dp[i] = Math.max(dp[i - 1], 0) + nums[i];
            max = Math.max(dp[i], max);
        }
        return max;
    }
}

标签:nums,int,max,len,Math,数组,1905,LeetCode,dp
来源: https://blog.csdn.net/Fitz1318/article/details/115707374