给一个数组,返回它的最大连续子序列的和。
{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。
O(n)时间复杂度的简洁解法
class Solution {
public:
int FindGreatestSumOfSubArray(vector<int> array)
{
int thepre=array[0];
int max=array[0];
for(int i=1;i<array.size();i++)
{
if(thepre<0)
thepre=0;
thepre+=array[i];
if(thepre>max)
max=thepre;
}
return max;
}
};