[LeetCode] Maxium Subarray

dynamic programming 的一道题。curMax是包括当前元素的subarray的max,
gloMax是当前元素之前的整个array的max

public class Solution {
    public int maxSubArray(int[] A) {
        if(A.length == 1) {
            return A[0];
        }
        if(A.length == 0) {
            return 0;
        }
        int curMax = A[0];
        int gloMax = A[0];
        for(int i = 1;i<A.length;i++){
            curMax = Math.max(curMax+A[i], A[i]);
            gloMax = Math.max(gloMax, curMax);
        }
        return gloMax;
    }

}
posted on 2015-03-31 00:25  Seth_L  阅读(111)  评论(0编辑  收藏  举报