标签:class blog java http tar com
问题描述:
输入一组整数,求出这组数字子序列和中最大值。也就是只要求出最大子序列的和,不必求出最大的那个序列。例如:
序列:-2 11 -4 13 -5 -2,则最大子序列和为20。
序列:-6 2 4 -7 5 3 2 -1 6 -9 10 -2,则最大子序列和为16。
算法一:
//穷举法,复杂度O(n^3)
long maxSubSum1(const vector<int>& a)
{
long maxSum = 0;
for (int i = 0; i < a.size(); i++)
{
for (int j = i; j < a.size(); j++)
{
long thisSum = 0;
for (int k = i; k <= j; k++)
{
thisSum += a[k];
}
if (thisSum > maxSum)
maxSum = thisSum;
}
}
return maxSum;
}
Java版本:http://zhuyanfeng.com/archives/438
标签:class blog java http tar com
原文地址:http://www.cnblogs.com/larrylawrence/p/3810913.html