剑指offer刷题记录30——连续子数组的最大和

非常简单的if-else

public class Solution {
    public int FindGreatestSumOfSubArray(int[] array) {
        if(array.length == 0) {
            return 0;
        }
        int temp = 0;
        int max = Integer.MIN_VALUE;
        for(int x : array) {
            if(temp < 0) {
                temp = x;
            } else {
                temp += x;
            }
            max = Math.max(temp, max);
        }
        return max;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_37684824/article/details/86562226