题解 | #子数组最大连续和#
子数组最大连续和
http://www.nowcoder.com/questionTerminal/1718131e719746e9a56fb29c40cc8f95
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
int n = input.nextInt();
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = input.nextInt();
}
System.out.print(maxSumArr(arr));
}
}
public static long maxSumArr(long[] arr) {
long pre = 0, maxSum = arr[0];
for(long x:arr) {
pre = Math.max(pre+x, x);
maxSum = Math.max(maxSum, pre);
}
return maxSum;
}
}