按照以往的习惯以为和最起码大于0,没想到测试的时候这道题还能出个小于0的,所以稍微改了改以前的代码,将sum的初始值置为A[0],就可以了
import java.util.*; public class MaxSum { public int getMaxSum(int[] A, int n) { // write code here int sum = A[0]; int temp = 0; for (int i = 0; i < n; i++) { if (temp > 0) { temp += A[i]; } else { temp = A[i]; } if (sum < temp) { sum = temp; } } return sum; } }