leetcode_152: Maximum Product Subarray

题目:Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.
Example 1:
Input: [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.
Example 2:
Input: [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
方法一:
由于是整型数组,所以不用考虑绝对值小于1的情况,只需要区分正数,负数,0三种情况。这里把中间变量初始值都设置为了0,所以在用中间变量的时候需要先判断,再与数组元素相乘(这里可以优化设置为1,减少后面的判断)。采用一次遍历, 每次遍历都记录下此时正的最大乘积和负的最大乘积,以及所有已遍历数组元素的最大乘积。

class Solution {
public:
    int maxProduct(vector<int>& nums){ 
        if(nums.size() < 2)
            return nums[0];

        int posValue=0;
        int savValue=0;
        int negValue=0;
        for(int i = 0; i< nums.size(); ++i)
        {
            if(nums[i]>0)
            {
                if(negValue)
                {
                    negValue = negValue * nums[i];
                }

                posValue = posValue? posValue * nums[i] : nums[i];
                savValue = posValue>savValue? posValue : savValue;
                
            }
                
            else if(nums[i]<0)
            {
                if(negValue == 0)
                {
                    negValue = posValue? posValue * nums[i] : nums[i]; 
                    savValue = posValue>savValue? posValue : savValue;
                    posValue = 0;
                }
                else
                {
                    int tempPos = posValue;
                    posValue = negValue * nums[i]; 
                    negValue = tempPos? tempPos * nums[i]: nums[i];
                    savValue = posValue>savValue? posValue : savValue;
                } 
            }
            else
            {
                savValue = posValue>savValue? posValue : savValue;
                posValue = 0;
                negValue = 0;
            }           
        } ;
    return savValue;
    }
};

方法二:先顺着遍历一遍, 在倒着遍历一遍, 确保所有的可能被考虑到。此方法和方法一一样存在一定的局限, 如果不是int序列
,就会出错。

class Solution {
public:
    int maxProduct(vector<int>& nums) {
        int max = nums[0], multi = 1,length=nums.size();
        //顺着遍历(连续奇数个|x|>=1的负数相乘得到的最大的乘积和遍历顺序有关)
        for (int i = 0; i < length; ++i)
        {
            multi *= nums[i];
            max = multi > max ? multi : max;   //可以改成if
            multi = multi == 0 ? 1 : multi;      //可以改成if
        }
        multi = 1;
        for (int i =length-1; i >=0;--i)
        {
            multi *= nums[i];
            max = multi > max ? multi : max;   //可以改成if
            multi = multi == 0 ? 1 : multi;      //可以改成if
        }
        return max;
    }
};

如果需要判断的不一定为int数组, 上述两种方法还需要把负数和绝对值<1的数拿出来做判断, 或者采用暴力求解, 利用动态规划的方法计算出每一步的最优值,再遍历出最大值。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值