题解 | #连续子数组的最大和#

数字在升序数组中出现的次数

http://www.nowcoder.com/practice/70610bf967994b22bb1c26f9ae901fa2

搜索问题的边界处理非常麻烦,要考虑出口,也要考虑是否越界等等。

class Solution {
public:
    int FindGreatestSumOfSubArray(vector<int> array) {
        int len = array.size();
        vector<int> dp;
        int max = array[0];
        dp.push_back(array[0]);
        for(int i=1; i < len; i++){
            int newMax = dp[i-1] + array[i];
            if(newMax > array[i]) dp.push_back(newMax);
            else dp.push_back(array[i]);
            if(dp[i] > max) max = dp[i];
        }
        return max;
    }

};
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务