一个有N个整数元素的一维数组{A[0],A[1],....,A[N-1],A[N]},这个数组有很多连续的子数组,那么连续子数组之和的最大的一个的值是什么?
时间:2017-06-18 23:22:11
收藏:0
阅读:834
1.动态规划的思想解决
/** * 在时间复杂度为O(N)内找出数组中最大的子序列的累加和 */ public static int sumNum(int[] array) { int n = array.length; int all = array[n - 1], start = array[n - 1]; int count = 0; for (int i = n - 2; i >= 0; i--) { if ((start + array[i]) > array[i]) { start = start + array[i]; } else { start = array[i]; } if (all < start) { all = start; } count++; } System.out.println("数组长度:" + array.length + ", 时间复杂度:" + count); System.out.println("最大值" + all); return all; }
原文:http://www.cnblogs.com/cbySense/p/7045771.html
评论(0)