53.求最大的子序列和

1.Question

2.Solution

public class Test{
    public static void main(String[] args) {
        int[] array = {-2, 11, -4, 13, -5, -2};
        System.out.println(maxSubArray(array));
    }


    public static int maxSubArray(int[] nums) {
        int len = nums.length;
        int max = Integer.MIN_VALUE;
        int temp = Integer.MIN_VALUE;
        
        for (int i=0; i<len; ++i){
            //遇到不是负数的开始加,负责只是比较max,从前面的数中找到最大的负数
            if (temp < 0)
                temp = nums[i];
            else{
                temp += nums[i];
            }
            if (temp > max){
                max = temp;
            }
        }
        
        return max;
    }
}

 

3.Test

posted @ 2016-07-15 23:10  桃源仙居  阅读(113)  评论(0)    收藏  举报