首页 > 其他 > 详细

53. Maximum Subarray

时间:2019-07-26 23:39:00      阅读:151      评论:0      收藏:0      [点我收藏+]

description:

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Note:

Example:

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

answer:

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int res = INT_MIN, cur = 0;
        for (int num: nums) {
            cur = max(cur + num, num);
            res = max(cur, res);
        }
        return res;
    }
};

relative point get√:

hint :

follow up:分治法

53. Maximum Subarray

原文:https://www.cnblogs.com/forPrometheus-jun/p/11253147.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!