给定一个整数数组,找到一个具有最小和的子数组。返回其最小和。
样例
给出数组[1, -1, -2, 1],返回 -3
public class Solution {
/*
* @param nums: a list of integers
* @return: A integer indicate the sum of minimum subarray
*/
public int minSubArray(List<Integer> nums) {
// write your code here
int sum = 0;
int min = nums.get(0);
for (int i = 0; i < nums.size(); i++) {
sum += nums.get(i);
if(sum < min){
min = sum;
}
if (sum > 0) {
sum = 0;
}
}
return min;
}
}