Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has the largest sum = 6.
问题描述:求最大连续子串的和 在程序中用local_max=max(local_max,nums[i])来表示局部最大和 global_max=max(local_max,global_max)来表示最大连续子串的和
class Solution {
public:
int maxSubArray(
vector<int>& nums) {
if(nums.size()==NULL)
return 0;
int n=nums.size();
int local_max=nums[
0];
int global_max=nums[
0];
for(
int i=
1;i<n;i++){
local_max=max(local_max+nums[i],nums[i]);
global_max=max(global_max,local_max);
}
return global_max;
}
};
转载请注明原文地址: https://ju.6miu.com/read-3042.html