c++ solution


  • 0
    A

    class Solution {
    public:
    int maxSubArray(vector<int>& nums) {

        if(nums.size() == 0)
            return 0;
        else
        {
            int curSums = nums[0];
            int maxSums = nums[0];
            
            for(int i = 1 ;i < nums.size();++i)
            {
                curSums += nums[i];
                
                if(curSums <= nums[i])//负收益 或 无收益
                {
                    curSums = nums[i];
                }
                
                if(curSums > maxSums)
                {
                    maxSums = curSums;
                }
                
            }
            
            
            return maxSums;
        }
        
        
        
        
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.