连续子数组的最大和(java)

public class demo55 {
  public static int maxSubArray(int[] nums) {
    int[][] p = new int[nums.length][nums.length];
    int k = nums[0];
    for (int i = 0; i < p.length; i++) {
      for (int j = 0; j < p[i].length; j++) {
        if (i > j) {
          p[i][j] = 0;
          continue;
        }
        if (i == j) {
          p[i][j] = nums[j];
          k = Math.max(k, p[i][j]);
          continue;
        }
        p[i][j] = p[i][j - 1] + nums[j];
        k = Math.max(k, p[i][j]);
      }
    }
    return k;
  }

  public static void main(String[] args) {
    int[] nums = {-100, -100, -16, -97, -67, -16, -3,
        -59, -21, -20, -65, -33, -79, -43, -17, -3, -89,
        -59, -70, -73, -16, -34, -39, -57, -34, -99, -4,
        -41, -37, -32, -13, -93, -21, -69, -76, -4, -34,
        -4, -91, -20, -94, -43, -50, -49, -18, -2, -87,
        -8, -79, -43, -3, -96, -92, -70, -97, -99, -1,
        -70, -74, -75, -84, -25, -87, -48, -15, -26, -99,
        -61, -94, -51, -59, -95, -48, -55, -7, -1, -57,
        -62, -24, -55, -36, -60, -55, -48, -43, -98,
        -92, -47, -20, -38, -98, -25, -99, -13, -70,
        -44, -94, -25, -4, -18};
    System.out.println(maxSubArray(nums));
  }
}

你可能感兴趣的:(连续子数组的最大和(java))