8. Maximum Subarray

// remmeber the begin and the end
//return the array

class Solution {
public:
int maxSubArray(vector& nums) {
    int sum = 0;
    int max = 0;
    int count = nums.size();
    for(int i=0;i0){
            max = max>sum?max:sum;
        }else{
            
            max = sum;
            sum=0;
        }                        
    }
    return max;
}
};

在【-1 -2】的时候,返回的是-2,结果是不对的

正确结果

class Solution {
public:
int maxSubArray(vector& nums) {
    int sum = 0;
    int smax = INT_MIN;
    int count = nums.size();
    for(int i=0;ismax) smax = sum;
        if(sum<0)sum=0;
    }
    return smax;
}
};

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