其他分享
首页 > 其他分享> > 连续子数组的最大和

连续子数组的最大和

作者:互联网

简介

刷题

参考链接

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;
    }
};

标签:www,return,最大,int,curSum,greatestSum,连续,数组,array
来源: https://www.cnblogs.com/eat-too-much/p/15124723.html