
给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
子数组 是数组中的一个连续部分。
int maxSubArray(int* nums, int numsSize)
{
int count = 0;
int max = nums[0];
for(int i = 0;i
{
count+=nums[i];
if(count>max)
{
max = count;
}
if(count<0)\当count小于0,从新开始记录子数组
{
count = 0;
}
}
return max;
}
自写,超过时间限制
int maxSubArray(int* nums, int numsSize){
int max = nums[0],sum;
for(int j =0;j
{
sum = nums[j];
for(int i = j+1;i
{
sum+=nums[j];
if(sum>max)
{
max = sum;
}
}
}
return sum;
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)