标签:
输入一个整数数组,数组里有整数也有负数。数组中一个或连续的多个整数组成一个数组。求所有子数组的和的最大值。要求时间复杂度为O(n)
public class Solution {
public int FindGreatestSumOfSubArray(int[] array) {
if (array == null || array.length == 0) {
return 0;
}
int curSum = 0;
int sum = 0x80000000;
for (int i = 0; i < array.length; i++) {
if (curSum < 0) {
curSum = array[i];
} else {
curSum += array[i];
}
if (curSum > sum) {
sum = curSum;
}
}
return sum;
}
}
标签:
原文地址:http://www.cnblogs.com/rosending/p/5637863.html