累加的子数组和,如果大于零,那么我们继续累加就行;否则,则需要剔除原来的累加和重新开始.
class Solution {
public:
int FindGreatestSumOfSubArray(vector<int> array) {
if(!array.size())
return 0;
int max = array[0],cur = array[0];
for(int i = 1;i<array.size();++i)
{
if(cur<=0)
cur = array[i];
else
cur+=array[i];
if(max<cur)
max = cur;
}
return max;
}
};
网友评论