码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode5.18

时间:2020-05-18 14:09:30      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:rod   algo   class   name   names   solution   pac   size   vector   

152.乘积最大数组

include

using namespace std;

include

include

class Solution {
public:
int maxProduct(vector& nums) {
int len = nums.size();
vectorvmax;
vectorvmin;
vmax.push_back(1);
vmin.push_back(1);
for (int i = 0; i < len; i++)
{
int tmp = nums[i];
if (tmp >= 0)
{
vmax.push_back( max(vmax[i] * nums[i], nums[i]));
vmin.push_back ( min(vmin[i] * nums[i], nums[i]));
}
else
{
vmax.push_back( max(vmin[i] * nums[i], nums[i]));
vmin.push_back(min(vmax[i] * nums[i], nums[i]));
}

    }
    int res = vmax[1];
    for (int j = 1; j < len + 1; j++)
    {
        if (vmax[j] > res)res = vmax[j];
    }
    return res;


}

};

leetcode5.18

标签:rod   algo   class   name   names   solution   pac   size   vector   

原文地址:https://www.cnblogs.com/wfplingyun/p/12910054.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!