53. Maximum Subarray

Problem

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

Example

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

Code

static int var = [](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();
class Solution {
public:
    int maxSubArray(vector& nums) {
        int max = INT_MIN;
        int sum = 0;
        for (int i = 0; i

Result

53. Maximum Subarray.png

你可能感兴趣的:(53. Maximum Subarray)