31、连续子数组最大和(动态规划)


            max=Math.max(max+array[i], array[i]);
            res=Math.max(max, res);
 

public class Solution {
   public  int FindGreatestSumOfSubArray(int[] array) {
        int res = array[0]; //记录当前所有子数组的和的最大值
        int max=array[0];   //包含array[i]的连续数组最大值
        for (int i = 1; i < array.length; i++) {
            max=Math.max(max+array[i], array[i]);
            res=Math.max(max, res);
        }
        return res;
       
    }    
}

猜你喜欢

转载自blog.csdn.net/lupa1521/article/details/90141332