题解 | #连续子数组的最大和#

连续子数组的最大和

https://www.nowcoder.com/practice/459bd355da1549fa8a49e350bf3df484

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

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务