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
# 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