【剑指Offer】42. 连续子数组的最大和

NowCoder

题目描述

{6, -3, -2, 7, -15, 1, 2, 2},连续子数组的最大和为 8(从第 0 个开始,到第 3 个为止)。

解题思路

public class Solution {
    public int FindGreatestSumOfSubArray(int[] array) {
        if(array.length == 0 || array == null)
            return 0;
        
        int curmax = Integer.MIN_VALUE;
        int sum = 0;
        for(int i : array) {
            sum = sum<=0 ? i : sum+i;
            curmax = Math.max(sum, curmax);
        }
        return curmax;
    }
}

你可能感兴趣的:(#,刷题)