这道题求的是乘积的最大值的,那么依照之前的和的最大值类似的做法的,乘积的最大值可能是在最大值*当前值和最小值*当前值和当前值三者之间取得的最大值的,那么使用两个变量来保存每一步的最大最小值的。

class Solution {
public:
int maxProduct(vector<int>& nums) {
if(nums.size()<=) return ;
int maxn,minn,res;
maxn=minn=res=nums[];
for(int i=;i<nums.size();i++){
if(nums[i]>){
maxn=max(maxn*nums[i],nums[i]);
minn=min(minn*nums[i],nums[i]);
}else{
int t=maxn; // 注意这里改变了最大值的
maxn=max(minn*nums[i],nums[i]);
minn=min(t*nums[i],nums[i]);
}
res=max(maxn,res);
}
return res;
}
};

从上面看出还可以更简短代码一些的,当小于0的时候就可以直接进行交换最大和最小值的,也不需要利用中间变量来重新替换的。

class Solution {
public:
int maxProduct(vector<int>& nums) {
if(nums.size()<=) return ;
int maxn,minn,res;
maxn=minn=res=nums[];
for(int i=;i<nums.size();i++){
if(nums[i]<) swap(maxn,minn);
maxn=max(maxn*nums[i],nums[i]);
minn=min(minn*nums[i],nums[i]);
res=max(maxn,res);
}
return res;
}
};

leetcode 152. Maximum Product Subarry的更多相关文章

  1. [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  2. [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  3. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

  4. LeetCode 152. Maximum Product Subarray (最大乘积子数组)

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

  5. Java for LeetCode 152 Maximum Product Subarray

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

  6. leetcode 152. Maximum Product Subarray --------- java

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

  7. C#解leetcode 152. Maximum Product Subarray

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

  8. [leetcode]152. Maximum Product Subarray最大乘积子数组

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  9. Leetcode#152 Maximum Product Subarray

    原题地址 简单动态规划,跟最大子串和类似. 一维状态空间可以经过压缩变成常数空间. 代码: int maxProduct(int A[], int n) { ) ; ]; ]; ]; ; i > ...

随机推荐

  1. topcoder srm 570 div1

    problem1 link 找到周期,每个周期的增量是相同的. problem2 link 对于分给某一个公司的有$c$个联通分量,其中$k$个联通分量只有1个节点,$c$个联通分量一共有$x$个节点 ...

  2. 通过Python计算一个文件夹大小

    在进行计算一个文件夹内容大小的时候,我们要考虑文件夹内都有什么内容,可能都是一个一个的单文件,也有可能都是子文件夹,或者二者都有,既然要计算整个文件夹的大小,我们当然要计算每一个文件的大小以及每一个子 ...

  3. spring(aop面向切面编程)

    aop很早有研究过,但是最近想回顾下,顺便记录下,aop的优点有很多,实用性也很广,就好比最早在公司没有使用aop的时候没个业务层都要写try catch来捕获异常,来处理异常,甚至于记录异常或者日志 ...

  4. css及HTML知识点

    html : 180°  输出为 css:    margin: 0 auto;会在页面水平居中显示  box-shadow: 0 0 5px #f61818; 设置投影的位置大小颜色 outline ...

  5. CKEditor富文本编辑器

    CKEditor 富文本即具备丰富样式格式的文本.在运营后台,运营人员需要录入课程的相关描述,可以是包含了HTML语法格式的字符串.为了快速简单的让用户能够在页面中编辑带格式的文本,我们引入富文本编辑 ...

  6. 『TensorFlow』SSD源码学习_其七:损失函数

    Fork版本项目地址:SSD 一.损失函数介绍 SSD损失函数分为两个部分:对应搜索框的位置loss(loc)和类别置信度loss(conf).(搜索框指网络生成的网格) 详细的说明如下: i指代搜索 ...

  7. openLDAP环境搭建

    OpenLDAP搭建   PS:本次实验是基于centos7,OpenLDAP使用2.4.44版本. 一.初始化环境 1.初始化环境 命令如下: ntpdate -u ntp.api.bz & ...

  8. babel-plugin-import配置babel按需引入antd模块,编译后报错.bezierEasingMixin()

    用create-react-app做项目的时候,同时引入了antd,为了实现按需加载antd模块,用到他们提供的  babel-plugin-import  ( 一个用于按需加载组件代码和样式的 ba ...

  9. Oracle DB 使用RMAN恢复目录

    • 对恢复目录和RMAN 资料档案库控制文件的使用进行比较• 创建和配置恢复目录• 在恢复目录中注册数据库• 同步恢复目录• 使用RMAN 存储脚本• 备份恢复目录• 创建虚拟专用目录 RMAN 资料 ...

  10. MQTT简介

    MQTT简介 MQTT是IBM开发的一个即时通讯协议,该协议支持所有的平台,几乎可以把所有联网的物品和外部连接起来,被用来当做传感器和致动器(比如通过Twitter让房屋联网)的通信协议 MQTT的特 ...