面试题:求最大子数组的合以及起始终止位

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 + "]");

    }

  

posted @ 2018-11-26 15:07  showme1942  阅读(187)  评论(0编辑  收藏  举报