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; }
时间: 2024-10-12 16:20:50