Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.

Example 1:

Input: [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.

Example 2:

Input: [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.



class Solution {
public:
int maxProduct(vector<int>& nums) {
int ans=nums[],frontprod=,backprod=;
int n=nums.size();
for(int i=;i<n;i++){
frontprod*=nums[i];
backprod*=nums[n-i-];
ans=max(ans,max(frontprod,backprod));
frontprod=frontprod == ? :frontprod;
backprod=backprod == ? : backprod; }
return ans;
}
};

152.[LeetCode] Maximum Product Subarray的更多相关文章

  1. LeetCode Maximum Product Subarray(枚举)

    LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...

  2. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

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

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

  4. LeetCode(152) Maximum Product Subarray

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

  5. Leetcode Maximum Product Subarray

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

  6. [LeetCode] Maximum Product Subarray 连续数列最大积

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

  7. [leetcode]Maximum Product Subarray @ Python

    原题地址:https://oj.leetcode.com/problems/maximum-product-subarray/ 解题思路:主要需要考虑负负得正这种情况,比如之前的最小值是一个负数,再乘 ...

  8. LeetCode Maximum Product Subarray 解题报告

    LeetCode 新题又更新了.求:最大子数组乘积. https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子 ...

  9. LeetCode Maximum Product Subarray 最大子序列积

    题意:给一个size大于0的序列,求最大的连续子序列之积.(有正数,负数,0) 思路:正确分析这三种数.0把不同的可能为答案的子序列给隔开了,所以其实可以以0为分隔线将他们拆成多个序列来进行求积,这样 ...

随机推荐

  1. 设置eclipse默认编码格式

    Window->Preferences->General ->Content Type->Text->JSP 最下面设置为UTF-8 Window->Prefere ...

  2. Spring boot Mybatis整合构建Rest服务(超细版)

     Springboot+ Mybatis+MySql整合构建Rest服务(涵盖增.删.改.查) 1.概要 1.1 为什么要使用Spring  boot? 1.1.1 简单方便.配置少.整合了大多数框架 ...

  3. CentOS7.6离线安装Tomcat8.5

    准备好tomcat安装文件: 官网下载apache-tomcat-8.5.39.tar.gz文件并复制到/usr/tomcat文件夹中. 解压tomcat安装文件: 进入/usr/tomcat文件:c ...

  4. 【原创】CRM 2015/2016,SSRS 生成PDF文件,幷以附件的形式发送邮件

    主要步骤如下: 生成一条邮件记录 生成一条ActivityParty记录 生成PDF文件,并以Base64添加到ActivityMimeAttachment 中去 打开发送邮件窗口,以便编辑及发送邮件 ...

  5. 【四】搭建Nginx服务器

    [任务4]搭建Nginx服务器 [任务4]搭建Nginx服务器 下载Nginx源码包 安装Nginx 解压Nginx安装包 安装Nginx依赖 启动Nginx 安装cgi 下载cgi并解压安装包 编译 ...

  6. Python-条件语句和循环语句

    ·条件语句 笔记: If  布尔值: print(‘hello,world!’) 当表达式为布尔表达式时,Flase   None  0   ””    ()    []    {} 都视为假! @ ...

  7. [Golang学习笔记] 04 程序实体1 变量声明

    变量声明: Go语言的程序实体包含:变量.常量.函数.结构体和接口,是一门静态类型的编程语言. (在声明变量或常量的时候,需要指定类型,或者给予足够信息是的Go语言能够推导出类型) Go语言变量的类型 ...

  8. Gblocks命令行

    使用默认的设置: $ Gblocks proteins.fasta -t=p 必须是 fasta 文件在前,参数在后.若没有参数,则进入交互式界面. Gblocks cds.fasta −t=c −b ...

  9. 消除运行MATLAB生成独立可执行程序的DOS黑屏

    基于Matlab生成独立可执行文件后,每次运行都存在DOS黑屏问题,可通过以下方法解决: 在Matlab命令窗口中输入: cd(prefdir) edit compopts.bat 在打开的文件最后添 ...

  10. JVM内存模型(转载)

    原文地址:http://blog.csdn.net/u012152619/article/details/46968883 JVM定义了若干个程序执行期间使用的数据区域.这个区域里的一些数据在JVM启 ...