Maximum Product Subarray 最大连续乘积子集
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 最大连续乘积子集的更多相关文章
- Maximum Product Subarray(最大连续乘积子序列)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- lintcode 中等题 :Maximum Product Subarray 最大连续乘积子序列
题目 乘积最大子序列 找出一个序列中乘积最大的连续子序列(至少包含一个数). 样例 比如, 序列 [2,3,-2,4] 中乘积最大的子序列为 [2,3] ,其乘积为6. 解题 法一:直接暴力求解 时 ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 46.Maximum Product Subarray(最大乘积子数组)
Level: Medium 题目描述: Given an integer array nums, find the contiguous subarray within an array (con ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- 【刷题-LeetCode】152 Maximum Product Subarray
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
- LeetCode_Maximum Subarray | Maximum Product Subarray
Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...
随机推荐
- Go语言容器
Map 是一种无序的键值对的集合.Map 最重要的一点是通过 key 来快速检索数据,key 类似于索引,指向数据的值. Map 是一种集合,所以我们可以像迭代数组和切片那样迭代它.不过,Map 是无 ...
- [转] linux alias 编写 函数 脚本
[From] https://blog.csdn.net/csdnmonkey/article/details/53286314 案例 alias ttt='ttt(){ echo $1 ; };tt ...
- javascript中操作元素属性
1. setAttribute():设置属性的值: getAttribute():得到属性的值: removeAttribute():移除属性: 2.offsetWidth:offsetWidth = ...
- reifiable type与raw type
下面的逻辑需要明白如下两个概念: 4.7. Reifiable Types 4.8. Raw Types 举几个是Reifiable Types的例子,如下: class A{} class B< ...
- 【开源组件】FastDFS极速入门与安装
FastDFS是一个开源的轻量级的分布式文件系统,为互联网量身定制,充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供 ...
- weblogic升级之ddconverter
1. weblogic8.x 升到weblogic10时,需要升级ejb响应的描述符,否则会报错. BEA-011114 - Error: For EJB modules, deployment pl ...
- webpack 报错 No PostCSS Config found 解决方案。
webpack 报错 No PostCSS Config found 这个问题我在百度上找了好久,也没有找到解决方案,最后没有办法只能自己去啃官方文档,本案例在本人的webpack 学习感悟中已经写 ...
- 九: 操作提示(js版本)
一.toast 消息提示框 他用到了一个wx.showToast(object) 这样一个方法.作用是显示提示框. /* ---page/test/test.wxml----*/ <butt ...
- [转]如何选择Html.RenderPartial和Html.RenderAction
Html.RenderPartial与Html.RenderAction这两个方法都是用来在界面上嵌入用户控件的. Html.RenderPartial是直接将用户控件嵌入到界面上: <%Htm ...
- [编程] C语言循环结构计算π的值
公式: 分析:首先,系数为正数的项的分母是4n-3(n为正数项的项数),为负数的项的分母为4n-1(n为负数项的项数),即分母的变化规律是1.3.5.7...的奇数数列,则第n项的分母为2n-1,第1 ...