标签:for math res [1] 位置 ati static [] nbsp
num在遍历数组过程中表示遍历到当前位置子数组和的最大值
public class Main { public static void main(String[] args) { int[] a = new int[8]; a[0] = 3; a[1] = -2; a[2] = 1; a[3] = -6; a[4] = 3; a[5] = 2; a[6] = -1; a[7] = 3; System.out.println(res(a)); } public static int res(int[] a) { if(a == null || a.length <= 0) return 0; int num = a[0]; int cur = a[0]; for (int i = 1; i < a.length; i++) { cur = cur < 0 ? 0 : cur; cur += a[i]; num = Math.max(num, cur); } return num; } }
标签:for math res [1] 位置 ati static [] nbsp
原文地址:http://www.cnblogs.com/cdx19971126/p/6146008.html