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

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. Log4j配置记录

    log4j的配置文件就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息.通过配置,可以创建出Log4J的运行环境. 1.配置文件 log4j配置文件的基 ...

  2. kvm日常维护

    1.列出物理机器上安装的虚拟[root@localhost ~]# virsh list --all Id Name State------------------------------------ ...

  3. QT下的darknet-GPU项目属性

    #------------------------------------------------- # # Project created by QtCreator 2018-08-04T19:39 ...

  4. Learning-Python【10】:函数初识

    一.什么是函数 函数就是代码的一种组织形式,是指将一组语句的集合通过一个名字(函数名)封装起来,要想指向这个函数,只需要调用其函数名即可 函数分为两大类:内置函数 和 自定义函数 二.为何要用函数 减 ...

  5. Lintcode481-Binary Tree Leaf Sum-Easy

    481. Binary Tree Leaf Sum Given a binary tree, calculate the sum of leaves. Example Example 1: Input ...

  6. Java 爬虫学习

    Java爬虫领域最强大的框架是JSoup:可直接解析具体的URL地址(即解析对应的HTML),提供了一套强大的API,包括可以通过DOM.CSS选择器,即类似jQuery方式来取出和操作数据.主要功能 ...

  7. 00-python-常用命令

    1. pip 加速命令 pip install --index-url https://pypi.douban.com/simple 或者 pip install -i https://pypi.tu ...

  8. 一个时代的终结:为什么是时候放弃ITOM四大巨头了?这对IT领导者来说意味着什么?

    ​​关注嘉为科技,获取运维新知 2018年7月,Broadcom宣布了收购CA Technologies的计划,收购额近190亿美元.虽然分析师对于芯片制造商收购企业级软件公司这件事是否值得还在进行激 ...

  9. dedecms 模版里格式化时间标签

    <!--带时分秒--> [field:pubdate function="GetDateTimeMK(@me)"/] <!--只有日期--> [field: ...

  10. asp.net core2.1 bundleconfig.json合并压缩资源文件

    在asp.net core中则可以使用BuildBundlerMinifier来进行css,js的压缩合并 1.使用NuGet安装 BuildBundlerMinifier(也可以在vs中下载安装扩展 ...