package algorithm;

/**
 * Created by yefeng on 16/10/22.
 */
public class MaxSubArray {

    public static int max(int a, int b) {
        return a > b ? a : b;
    }

    public static int maxSubArray(int[] array) {
        int nEnd = array[0];
        int nAll = array[0];
        for (int i = 0; i < array.length; ++i) {
            nEnd = max(nEnd + array[i], array[i]);
            nAll = max(nEnd, nAll);
        }
        return nAll;
    }

    public static void main(String[] args) {
        int[] arr = {1, -2, 4, 8, -4, 7, -1, -5};
        System.out.println(maxSubArray(arr));
    }
}

@@ -51,16 +51,28 @@
 /10/22.%0A
+ * %E6%B1%82%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E6%9C%80%E5%A4%A7%E5%92%8C%0A
  */%0Apubl
@@ -447,32 +447,659 @@
 rn nAll;%0A    %7D%0A%0A
+    public static int maxSubArray2(int%5B%5D array) %7B%0A        int maxSum = Integer.MIN_VALUE;%0A        int begin = 0, end = 0;%0A        int nStart = 0;%0A        int nSum = 0;%0A        for (int i = 0; i %3C array.length; ++i) %7B%0A            if (nSum %3C 0) %7B%0A                nSum = array%5Bi%5D;%0A                nStart = i;%0A            %7D else %7B%0A                nSum += array%5Bi%5D;%0A            %7D%0A            if (nSum %3E maxSum) %7B%0A                maxSum = nSum;%0A                begin = nStart;%0A                end = i;%0A            %7D%0A        %7D%0A%0A        System.out.println(String.format(%22start=%25d, end=%25d%22, begin, end));%0A        return maxSum;%0A%0A    %7D%0A%0A
     public stati

add files