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

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. java与javax的区别分析(转)

    Java是一种受C语言影响的编程语言.Java和Javax本质上是与Java编程语言的上下文一起使用的包.实际上Java和Javax没有区别.这只是不同的名字. Java是一种编程语言,受到C语言的影 ...

  2. 关闭vs的编译警告

    现象:出现warning cxxxx.. 解决:项目,属性,C/C++,高级,禁用特定警告,把xxxx输入

  3. AFNetworking的简单使用

    AFNetworking的下载地址: https://github.com/AFNetworking/AFNetworking AFNetworking的使用非常简单,创建一个类,调用一个方法就可以达 ...

  4. File 与 FileStream 文件运用

    using System.IO; using System.Text; using UnityEngine; /// <summary> /// File 一次性控制 (小文件) /// ...

  5. P3829 [SHOI2012]信用卡凸包

    思路 注意到结果就是每个信用卡边上的四个圆心的凸包周长+一个圆的周长 然后就好做了 注意平行时把距离小的排在前面,栈中至少要有1个元素(top>1),凸包中如果存在叉积为0的点也要pop,否则可 ...

  6. 【笔记】Cocos2dx学习笔记

    自建场景类 自建场景类BaseScene继承与Scene类,在init函数中添加了默认的,键盘与鼠标事件的响应,添加了一个用于读取XML文件的字典,添加了一个结束场景的方法. 类的声明代码如下: #i ...

  7. Pandas 基础(4) - 读/写 Excel 和 CSV 文件

    这一节将分别介绍读/写 Excel 和 CSV 文件的各种方式: - 读入 CSV 文件 首先是准备一个 csv 文件, 这里我用的是 stock_data.csv, 文件我已上传, 大家可以直接下载 ...

  8. js实现千位分隔

    最近一个项目中使用到了千位分隔这个功能,在网上也看见一些例子,但是实现起来总觉有些复杂.因此,自己实现了一个千位分隔,留给后来的我们. 先上源码吧. 该方法支持传入的是一个数字字符串,数字.第二个参数 ...

  9. 音乐推荐与Audioscrobbler数据集

    1. Audioscrobbler数据集 数据下载地址: http://www.iro.umontreal.ca/~lisa/datasets/profiledata_06-May-2005.tar. ...

  10. Lab 7-2

    Analyze the malware found in the file Lab07-02.exe. Questions and Short Answers How does this progra ...