42连续子数组的最大和

参考资料:
[1]牛客000001号的回答:
https://www.nowcoder.com/profile/4449089/codeBookDetail?submissionId=13532648

class Solution {
public:
    int FindGreatestSumOfSubArray(vector 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 array) {
    
        if(array.empty())
            return 0;
        int tempsum = array[0],sum = array[0];
        for(int i = 1;isum)?tempsum:sum;
        }
        return sum;
    }
};

你可能感兴趣的:(42连续子数组的最大和)