面试题:求最大子数组的合以及起始终止位
public void getMaxArray(int[] a) { int start = 0; int end = 0; int currentSum = 0; int maxSum = Integer.MIN_VALUE; int aStart = 0; for (int i = 0; i < a.length; i++) { currentSum += a[i]; if (a[i] > currentSum) { currentSum = a[i]; aStart = i; } if (currentSum > maxSum) { maxSum = currentSum; start = aStart; end = i; } } System.out.println("maxSum:" + maxSum + ",a[" + start + "]" + ",a[" + end + "]"); }
不积跬步无以至千里