Maximum sum of consecutive subarrays

Find the maximum sum of consecutive subarrays, with positive and negative array elements.

public class Solution {
    public int FindGreatestSumOfSubArray(int[] array) {
        if(array.length == 0 || array == null)
            return 0;
        int maxSum = Integer.MIN_VALUE;
        int curSum = 0;
        for(int i = 0; i < array.length; i++){
            if(curSum <= 0){
                curSum = array[i];    //小于0,累加后和减小
            }else{
                curSum += array[i];
            }
            if(curSum > maxSum)
                maxSum = curSum;
        }
        return maxSum;        
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325522969&siteId=291194637