leetcode :Find the contiguous subarray within an array (containing at least one number) which has th

题目:

Find the contiguous subarray within an array (containing at least one number) which has the largest product.

For example, given the array [2,3,-2,4],
the contiguous subarray [2,3] has the largest product = 6.

整理后为:

答案:

int maxProduct(int A[], int n)
{
	if (n == 1)
		return A[0];
	int max = INT_MIN;
	int negative_num = 0;
	int negative_first = 1;
	int negative = 1;
	int all = 1;
	bool positive_after_negative = false;
	for (int i = 0; i < n; i++)
	{
		if (A[i] != 0)
		{
			if (A[i] < 0)
			{
				negative_num++;
				if (negative_num == 1)
					negative_first = A[i];
	
				
			}
			else
			{
				if (negative_num == 1)
					positive_after_negative = true;
			}
			if (negative_num > 0)
				negative = A[i] * negative;
			
			all = all * A[i];
			if (all > max)
				max = all;
			if (negative_num > 0)
			{
				if (negative > 0)
				{
					if (negative > max)
						max = negative;
				}
				else
				{
					if (negative_num > 1 || positive_after_negative)
					{
						if (negative / negative_first > max)
							max = negative / negative_first;
					}
				}
			}
		}
		else
		{
			if (max < 0)
			{
				max = 0;
			}	
			negative_num = 0;
			negative_first = 1;
			negative = 1;
			all = 1;
			positive_after_negative = false;
		}
	}
	return max;
}



说明:

对于一个整数数组只存在0,小于0,大于0,三种数,有两种情况:

           1》对于不包含0的情况下:

                        只有正数和负数,这时就要考虑整个数组的乘积了,可以分为以下三种情况:

                                   a:有奇数个负数 ,比如leetcode中用来测试的样例:[3,-1,4]。

                                   b:有偶数个负数。

                                   c:只有正数。

                         对于这三种情况,我们可以用一个变量"all"记录全部数组的乘积,用一个变量"negative"来记录出现负数之后,从负数开始的之后所有数的乘积,用"negative_first"来记录第一个出现的那个负数,用"negative_num"来记录出现负数的总个数。如果数组中出现了负数即:"negative_num > 0"我们就要用"negative"和最大值"max"作比较,但在这个时候要注意,和"max"作比较只能是正数(关于第一个数是负数的情况可以由全部数组的乘积"all"来处理),如果"negative < 0"说明有奇数个负数,我们要去掉第一个负数,即:"negative / negative_first ;"然后再做比较,同时还要注意[0,-1]和[3,-1,4](这种情况在包含0的情况中再做讨论)的区别,所以我们设定当负数的个数大于1且为奇数时再做纠正工作,对于只有一个负数时另作讨论。

                             每添加一个新的(A[i] != 0)我们都要更新"all"、"negative"、"negative_num"、"negative"、"negative_first"(只更新一次)然后依据情况"all"和"max"作比较,然后是负数是否出现再做比较:"negative"和"max"  或  "negative / negative_first"和"max"。

             2》当0出现时。当0出现时,分3种情况:

                           a:"max"大于0。

                           b:"max"小于0。

                           c:"max"等于0。

                           当"max"小于或等于0时与0作比较肯定是0大,比较之后"max"等于0。当"max"大于0时肯定"max"是不变的。然后初始化"all"、"negative"、"negative_num"等变量。此后,对于0之后的数组元素A[i] != 0就可以依照情况一中再次进行处理。

                            对于[0,-1]和[3,-1,4]这种只出现一次负数的情况,如果复数后面没有数出现即:[0,-1],进行 "negative / negative_first"后可能会出错,于是针对负数之后又出现数的情况,我们用一个bool变量"positive_after_negative"来标记。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值