最大连续子数组和

一道经典的面试笔试题目。

第一种解法,遍历每种情况,算法复杂度为O(N^2)。

int maxSubarray1(int *A,int length)
{
	int maxSum=A[0];
	int temp;
	for (int i=0;i<length;i++)//从i开始的子数组
	{
		temp=0;
		for (int j=i;j<length;j++)
		{
			temp=temp+A[j];
			if (temp>maxSum)
			{
				maxSum=temp;
			}
		}
	}
	return maxSum;
}

第二种解法,利用二分法。把数组分为两段,最大子数组出现在1、左半段。2、右半段。3、横跨左右两半段。算法复杂度为O(NlogN)。

int MaxThree(int a, int b, int c)//求三个数中的最大数
{
	int temp = a>b? a: b;
	return temp>c? temp : c;
}
int maxSubArray2(int *A,int start,int end)
{
	if (start==end)
	{
		return A[start];
	}
	int mid=(start+end)/2;//找到中间

	//找从中间开始,左边连续最大和
	int maxLeftSum=A[mid];
	int temp=0;
	for (int k=mid;k>=start;k--)
	{
		temp=temp+A[k];
		if (temp>maxLeftSum)
		{
			maxLeftSum=temp;
		}
	}
	//从中间开始,找右边连续最大子数组
	int maxRightSum=A[mid+1];
	temp=0;
	for (int k=mid+1;k<=end;k++)
	{
		temp=temp+A[k];
		if (temp>maxRightSum)
		{
			maxRightSum=temp;
		}
	}
	return MaxThree(maxLeftSum+maxRightSum,maxSubArray2(A,start,mid),maxSubArray2(A,mid+1,end));
}

第三种解法,只需要扫描一遍数组即可。在扫描过程中,记录连续子数组和,如果小于零,则重新开始记录。在记录过程中,找到最大的。算法复杂度为O(N)。

int maxSubArray3(int *A,int len)
{
	int maxSum=A[0];
	int temp=0;
	for (int k=0;k<len;k++)
	{
		temp=temp+A[k];
		if (temp>maxSum)
		{
			maxSum=temp;
		}
		if (temp<0)
		{
			temp=0;
		}
	}
	return maxSum;
}

测试代码:

int main()
{
	int A[]={4, -3, 2 ,-2 ,4 , -7, 6 , -2 ,7 ,-5 ,8 , -7, 3};
	int len=sizeof(A)/sizeof(int);
	cout<<maxSubarray1(A,len)<<endl;
	cout<<maxSubArray2(A,0,len-1)<<endl;
	cout<<maxSubArray3(A,len);
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值