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

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. Springboot 使用PageHelper分页插件实现分页

    一.pom文件中引入依赖 二.application.properties中配置以下内容(二选一方案) 第一种:pagehelper.helper-dialect=mysqlpagehelper.re ...

  2. [c/c++] programming之路(18)、动态分配内存malloc

    一.图解堆栈 #include<stdio.h> #include<stdlib.h> #include<Windows.h> void main0(){ **]; ...

  3. 剑指offer(35)数组中的逆序对

    题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P%1000 ...

  4. 【Python66--checkbutton&】

    一.定义:Checkbutton组件用于实现是否选择的按钮 二.作用:使用Checkbutton,必须创建一个tkinter变量用于存放按钮的状态:v=IntVar() from tkinter im ...

  5. The Guideline of Setting Up Samba Server on linux(Ubuntu)

    The Guideline of Setting Up Samba Server on linux(Ubuntu) From terminate command window, install the ...

  6. udp套接字及利用socketserver模块实现并发以及并发编程

    一:基于udp协议(数据报协议)的套接字:和tcp协议的套接字对比而言,由于udp是无链接的,所以先启动哪一端都不会报错,而且udp也不会有粘包 现象,所以对比下来,tcp协议的话传输数据更加可靠,但 ...

  7. 误操作yum导致error: rpmdb

    error: cannot open Packages index using db5 -  (-30973) error: cannot open Packages database in /var ...

  8. https 适配

    1info plist <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryL ...

  9. vxlan中vtep角色,以及通过GRE隧道进行流镜像

    1. 交换机上建立gre隧道,对端ip为ip12. 交换机上报gre隧道的OF逻辑端口port id,这里gre tunnel的id实际就是OF逻辑端口id3. 控制器建立流ipflow1的镜像配置, ...

  10. axios和promise

    什么是axios axios is a promise based HTTP client for the browser and node.js Features: Make XMLHttpRequ ...