int max(int i,int j){
if(i>j) return i;
return j;
}
int maxSubArray(int* nums, int numsSize) {
if(numsSize==1) return nums[0];
int* dp=(int*)malloc(sizeof(int)*numsSize);//从0到i,以nums[i]结尾的最大连续字串
dp[0]=nums[0];
int x=dp[0];
for(int i=1;i<numsSize;i++){
if(nums[i]>0){
dp[i]=max(dp[i-1]+nums[i],nums[i]);
if(dp[i]>x) x=dp[i];
}else{
dp[i]=max(dp[i-1]+nums[i],nums[i]);
if(dp[i]>x) x=dp[i];
}
}
return x;
}
结果:
标签:numsSize,return,最大,nums,int,max,53,数组,dp From: https://www.cnblogs.com/llllmz/p/18064630