task4: maximum subsequence and

Title #
## given the nums an array of integers, and find a maximum of the successive sub-array (sub-array comprising a least one element), and has returned to its maximum.
# Code

class Solution {public:    int maxSubArray(vector<int>& nums)     {   
     if(nums.empty())           
      return 0;//如果是空数组,则返回0      
        int presum = nums[0];//persum用来当前的和,maxsum用来记录最大的和       
         int maxsum = presum;        
         for(int i = 1; i < nums.size(); i++)        {       
              presum = presum > 0 ? presum += nums[i] : nums[i]; //如果persum<0,则将nums[i]赋值给persum            maxsum = max(maxsum,presum);         }       
               return maxsum;    }
               };

# The results of
Here Insert Picture Description
# idea of
## questions to find the maximum and continuous sub-arrays can be used to find the dynamic that defines two variables presum, maxsum, a dynamically selected to do addition, a record maximum

Released two original articles · won praise 0 · Views 55

Guess you like

Origin blog.csdn.net/qq_45877151/article/details/104594842