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

For example, given the array [2,3,-2,4],
the contiguous subarray [2,3] has the largest product = 6.

此题要求是要在一个无需的数组找出一个乘积最大的连续子数组

例如[2,3,-2,4],因为可能有负数,可以设置两个临时变量mint和maxt,mint存储遇到负数之后相乘变小的乘积,maxt用来存储大的乘积。

比如2*3=6,此时,mint = maxt = 6,当下次遇到-2时,mint = maxt = -12,此时乘积-12小于-2,则应使maxt = -2。为避免下个数还是负数,应使mint不变,若下次遇到负数,则乘积比maxt大,然后交换……

具体看代码:

 public class Solution {
public int maxProduct(int[] A) {
int n = A.length;
int mint = 1;
int maxt = 1; int maxvalue = A[0];
for(int i = 0 ; i < n ; i++){
if(A[i] == 0){
mint = 1;
maxt = 1;
if(maxvalue < 0)
maxvalue = 0;
}else{
int curmax = maxt * A[i];
int curmin = mint * A[i]; maxt = curmax > curmin ? curmax : curmin;
mint = curmax > curmin ? curmin : curmax; if(maxt < A[i])
maxt = A[i]; if(mint > A[i])
mint = A[i]; if(maxt > maxvalue)
maxvalue = maxt;
}
} return maxvalue; }
}

Maximum Product Subarray 最大连续乘积子集的更多相关文章

  1. Maximum Product Subarray(最大连续乘积子序列)

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

  2. lintcode 中等题 :Maximum Product Subarray 最大连续乘积子序列

    题目 乘积最大子序列 找出一个序列中乘积最大的连续子序列(至少包含一个数). 样例 比如, 序列 [2,3,-2,4] 中乘积最大的子序列为 [2,3] ,其乘积为6. 解题  法一:直接暴力求解 时 ...

  3. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

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

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

  5. 46.Maximum Product Subarray(最大乘积子数组)

    Level:   Medium 题目描述: Given an integer array nums, find the contiguous subarray within an array (con ...

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

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

  7. LeetCode Maximum Product Subarray(枚举)

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

  8. 【刷题-LeetCode】152 Maximum Product Subarray

    Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...

  9. LeetCode_Maximum Subarray | Maximum Product Subarray

    Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...

随机推荐

  1. js 弹性导航

    <style> *{margin:0;padding:0;} #box{height:50px;float:left;position:relative;background:#f90;} ...

  2. windows下几个方便的右键菜单

    一直在用,拷来拷去麻烦,贴这里方便自己方便大家 PSHere.reg 右键菜单打开powershell Windows Registry Editor Version 5.00 [HKEY_CLASS ...

  3. Windows开发经验 - Visual Studio 2017

    1. 调试子进程 Visual Studio 2017及更早的版本原生不支持调试子进程,不确定未来是否会支持.可以通过官方插件让Visual Studio能够调试子进程. https://market ...

  4. phpspreadsheet开发手记

    坑安装简单示例通过模板来生成文件释放内存单元格根据索引获取英文列设置值合并单元格居中显示宽度设置批量设置单元格格式直接输出下载自动计算列宽函数formula单元格变可点击的超链 PhpSpreadsh ...

  5. SVN无法读取cruuent修复方法

    解决方法:在网上百度和google了一大圈之后,终于得知是断电时current和txn-current文件没有写入当前最新版本号和最新版本的路径问题 当时非常抓狂,项目刷新一直为空. 1.先把curr ...

  6. yaf视图

    Yaf默认是开启了自动渲染,所以建了action后,他就会自己找模板!在测试的时候,如果不想让他寻找模板可以在action中return false 或者在bootstrap.php中关闭渲染 Yaf ...

  7. 实例说明optimize table在优化mysql时很重要

    今天在看CU的时候,发现有人问有关optimize来表优化的问题,当年因为这个问题,困扰我很长一段时间,今天有空我把这个问题,用实际数据来展示出来,让大家可以亲眼来看看,optimize table的 ...

  8. *2.3.2_加入env

    在验证平台中加入reference model.scoreboard等之前,思考一个问题:假设这些组件已经定义好了,那么在验证平台的什么位置对它们进行实例化呢?在top_tb中使用run_test进行 ...

  9. 前端工程师的mysql笔记

    背景 最近常参与后台php项目,虽说刚毕业时自学过一阵子php和mysql,不过长时间没用也忘差不多了,于是把mysql再温习一遍,前端同学也可以一起学习下! mysql安装以及操作 安装 brew ...

  10. Firebird 安装多实例

    火鸟数据库的安装向导,默认不允许多实例. 但是不管出于什么原因,若想安装多实例,很简单. 1.先用安装文件,按照向导安装第一个实例. 2.安装后不要启动服务,根据需要配置好Firebird.conf. ...