Atraditional greedy approach.Keep current sum, slide from left to right, when sum < 0, reset sum to 0.intmaxSubarray(int a[], int size) { if (size<=0) error(“error array size”); int sum = 0; int max = - (1 << 31); int cur = 0; while (cur < size) { sum += a[cur++]; if (sum > max) { max = sum; } else if (sum < 0) { sum = 0; } } return max;}
共 1 个关于本帖的回复 最后回复于 2013-8-16 10:29