smilevchy's blog

Life & Study & Chasing

Leetcode_Maximum Subarray

原题链接: https://oj.leetcode.com/problems/maximum-subarray/

public int maxSubArray(int[] A) {
    if (A == null || A.length == 0) return 0;

    int localMax = A[0];
    int globalMax = A[0];

    for (int i = 1, len = A.length; i < len; i++) {
        localMax = Math.max(A[i], localMax + A[i]);
        globalMax = Math.max(localMax, globalMax);
    }

    return globalMax;
}

algorithm

« Leetcode_Search Insert Position Leetcode_Single Number II »