leetcode152

Posted AsenYang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode152相关的知识,希望对你有一定的参考价值。

class Solution {
public:
    int maxProduct(vector<int>& nums) {
        if(nums.empty())
            return 0;
        if(nums.size() == 1)
            return nums[0];
            
        int maxAll = nums[0];       //global maximum
        int maxLast = nums[0];      //maximum including last element
        int maxCur;                 //maximum including current element
        int minLast = nums[0];      //minimum including current element
        int minCur;                 //minimum including last element
        for(int i = 1; i < nums.size(); i ++)
        {
            maxCur = max(nums[i], max(maxLast*nums[i], minLast*nums[i]));
            minCur = min(nums[i], min(maxLast*nums[i], minLast*nums[i]));
            maxLast = maxCur;
            minLast = minCur;
            maxAll = max(maxAll, maxCur);
        }
        return maxAll;
    }
};

 

以上是关于leetcode152的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode第152题—乘积最大子数组—Python实现

LeetCode152. 乘积最大子数组(DP)

leetcode 152. 乘积最大子序列

LeetCode 152. 乘积最大子数组c++/java详细题解

C#解leetcode 152. Maximum Product Subarray

leetcode152