连续子数组的最大和

简介

刷题

参考链接

https://www.nowcoder.com/practice/459bd355da1549fa8a49e350bf3df484?tpId=117

核心思想

遍历一遍得到结果

code

class Solution {
public:
    int FindGreatestSumOfSubArray(vector<int> array) {
        if(array.size() == 0) {
            return 0;
        }
        int curSum = 0;
        int greatestSum = 0x80000000;
        for(int i=0; i<array.size(); i++) {
            if(curSum <= 0) {
                curSum=array[i];
            } else{
                curSum+=array[i];
            }
            if(curSum > greatestSum) {
                greatestSum = curSum;
            }
        }
        return greatestSum;
    }
};
上一篇:leecode-842.将数组拆分成斐波那契数列


下一篇:【LeetCode-数组】最大子序和