连续子数组的最大和
题目描述
class Solution {
public:
int FindGreatestSumOfSubArray(vector<int> array) {
if(array.empty())
return 0;
int sum = 0;
int j = 0; //记录有array中有多少个负数
int num = array.size();
for(int i =0 ;i<num ;i++)
{
if(array[i] < 0)
j++;
int sum1 = 0;
for(int j = i;j<num;j++){
sum1 += array[j];
if(sum1> sum)
sum = sum1;
}
}
if(j == num)
{
int sum2 = array[0];
for(int a =0 ;a < num ;a++)
{
if(sum2 < array[a])
sum2 = array[a];
}
return sum2;
}
return sum;
}
};