剑指offer——30.连续子数组的最大和

问题描述:

{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。给一个数组,返回它的最大连续子序列的和

思路:

使用动态规划
直接对原数组进行迭代式修改:

图片说明

代码:

import java.lang.Math;
public class Solution {
    public int FindGreatestSumOfSubArray(int[] array) {
        int res = nums[0];
        for(int i = 1; i < nums.length; i++) {
            nums[i] += Math.max(nums[i - 1], 0);
            res = Math.max(res, nums[i]);
        }
        return res;
    }
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务