给定一个整数数组,找到一个具有最大和的子数组,返回其最大和。
public class Solution {
/*
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
public int maxSubArray(int[] nums) {
// write your code here
int cur = 0;
int max = Integer.MIN_VALUE;
for (int n : nums) {
cur += n;
cur = cur > n ? cur : n;
max = max > cur ? max : cur;
}
return max;
}
}