参考资料:
[1]牛客000001号的回答:
https://www.nowcoder.com/profile/4449089/codeBookDetail?submissionId=13532648
class Solution {
public:
int FindGreatestSumOfSubArray(vector<int> array) {
if (array.size() == 0)
return 0;
int nTmpSum = array[0], nSum = array[0];
for (int i = 1; i < array.size(); i++)
{
nTmpSum = (nTmpSum < 0) ? array[i] : (nTmpSum + array[i]);
nSum = (nSum > nTmpSum) ? nSum : nTmpSum;
}
return nSum;
}
};
标准答案:
class Solution {
public:
int FindGreatestSumOfSubArray(vector<int> array) {
if(array.empty())
return 0;
int tempsum = array[0],sum = array[0];
for(int i = 1;i<array.size();i++)
{
tempsum = (tempsum<0)?array[i]:tempsum+array[i];
sum = (tempsum >sum)?tempsum:sum;
}
return sum;
}
};