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. pycharm+gitee

    Git操作 前言: 由于各种原因,很多时候我们写代码的电脑并不会随身携带,所以有的时候突发灵感想继续写代码就变得难以实现.相信大部分同学对此都有了解,那就通过代码托管平台来管理.原本想用GitHub来 ...

  2. Array对象的判定

    /* 关于JS对象类型的判断,最复杂的在于RegExp和Array了,判定RegExp的情形不较少,而Array就比较多了,下面就是判断Array的方法 */ //方法一:利用instanceof来判 ...

  3. javascript006_Object_模拟java的Map

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  4. 【数组】Subsets

    题目: Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset ...

  5. 认识HDFS分布式文件系统

    1.设计基础目标 (1) 错误是常态,需要使用数据冗余  (2)流式数据访问.数据批量读而不是随机速写,不支持OLTP,hadoop擅长数据分析而不是事物处理.  (3)文件采用一次性写多次读的模型, ...

  6. SQL性能调优

    部分转自:http://www.cnblogs.com/luckybird/archive/2012/06/11/2544753.html 及http://www.cnblogs.com/kissdo ...

  7. 开始使用 Vuejs 2.0 ---简单总结2

    Vuejs的常用指令 v-html v-show v-if v-for v-on 1 .v-html v-html 更新元素或者变量的innerHTML,按普通html解析,和v-text的区别是在变 ...

  8. js 列表选择

    首选定义数组,然后进行操作时遍历数组获取选中值 function getSelect(userId) { //var userId = userCheckBox.value; //标记删除还是添加 v ...

  9. 转:详解PV、UV、VV、IP及其关系与计算

    一.什么是PV? PV即Page View,网站浏览量,指页面浏览的次数,用以衡量网站用户访问的网页数量.用户每次打开一个页面便记录1次PV,多次打开同一页面则浏览量累计.一般来说,PV与来访者的数量 ...

  10. Java中用双缓冲技术消除闪烁

    在Java编写具有连贯变化的窗口程序时,通常的办法是在子类中覆盖父类的paint(Graphics)方法,在方法中使用GUI函数实现窗口重绘的过程.连贯变换的窗口会不断地调用update(Graphi ...