LeetCode OJ 152. 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
.
【题目分析】
给定一个整数数组,找出数组中的一个子序列,使得序列中所有元素的乘积最大。
【思路】
观察发现数组中的元素可以分为三类:正数,负数和零。我们找到如下几种情况:
情况1:数组中没有0元素,而且数组中负数的个数为偶数。
解决方法:直接返回数组中各个元素的乘积;
情况2:数组中没有0元素,而且数组中负数的个数为奇数。
解决方法:计算第一个负数数之后所有元素的乘积pro1和最后一个负数之前的所有元素乘积pro2,返回较大值。
情况3:数组中含有0元素。
解决方法:用0元素把数组分为若干段,对每一段使用情况1和情况2中的方法进行处理。
【java代码】
public class Solution {
public int maxProduct(int[] nums) {
if(nums.length == 0) return 0;
int prezero = -1, curzero = -1;
int maxpro = Integer.MIN_VALUE; for(int i = 0; i < nums.length; i++){ //找到数组中的0元素,把数组分为不包含0的若干段进行处理
if(nums[i] == 0){
prezero = curzero;
curzero = i;
maxpro = Math.max(product(nums, prezero+1, curzero-1), maxpro);
}
}
if(curzero < nums.length - 1) maxpro = Math.max(product(nums, curzero+1, nums.length-1),maxpro); if(maxpro > 0 || curzero == -1) return maxpro; //如果最大值大于零或者数组中没有0,直接返回找到的最大值
else return 0; //否则返回0,此时maxpro<=0 && curzero != 0
} public int product(int[] num, int start, int end){
if(start > end || start < 0 || end < 0) return Integer.MIN_VALUE;
int firstneg = -1, lastneg = -1, pro = 1; //firstneg表示第一个负数的下标,lastneg表示最后一个负数的下标
for(int i = start; i <= end; i++){
pro *= num[i];
if(num[i] < 0){
if(firstneg == -1) firstneg = i;
lastneg = i;
}
}
if(pro > 0 || start == end) return pro; //如果找到的值大于零或者数组中只有一个元素则直接返回结果 int pro1 = pro, pro2 = pro; //否则计算第一个负数数之后所有元素的乘积pro1和最后一个负数之前的所有元素乘积pro2
for(int i = start; i <= firstneg; i++){
pro1 /= num[i];
}
for(int i = lastneg; i <= end; i++){
pro2 /= num[i];
}
return pro1 > pro2 ? pro1 : pro2; //返回较大值
}
}
效果:
LeetCode OJ 152. Maximum Product Subarray的更多相关文章
- 【刷题-LeetCode】152 Maximum Product Subarray
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
- [LeetCode]152. Maximum Product Subarray
This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...
- 【LeetCode】152. Maximum Product Subarray 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 动态规划 参考资料 日期 题目地址:htt ...
- 【LeetCode】152. Maximum Product Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- LeetCode OJ:Maximum Product Subarray(子数组最大乘积)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- 152. Maximum Product Subarray - LeetCode
Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
随机推荐
- LED的串联电阻值的计算
与LED串联的电阻被用于控制该LED导通时的电流量.为了计算电阻值,你需要知道输入电源电压(Vs,一般为5V),LED的正向电压(Vf)和你需要流过LED的电源(/)的数值. 其电阻欧姆值的计算公式( ...
- C# 操作IIS -App & AppPools
using System; using System.DirectoryServices; using System.Management; using Microsoft.Web.Administr ...
- [读书笔记]xampp mysql启动失败解析(win7)
1. [mysql] MySQL Service detected with wrong path [mysql] Change XAMPP MySQL and Control Panel s ...
- CUDA开发时用到的各种Linux命令
cat 读取文件中的全部内容. 例:cat cuda_add.cu
- 洞穴勘测(bzoj 2049)
Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...
- js数组中的注意
1.数组删除 2.数组合并 3.原数组会被修改的数组方法有: 1)排序 .sotr() 2)逆序 .reverse() 3)数组拼接 .splice()
- mysql随记
.frm是描述了表的结构,.MYD保存了表的数据记录,*.MYI则是表的索引 ibd是MySQL数据文件.索引文件,无法直接读取. ibdata是innodb引擎使用的 如果是使用myisam引擎 则 ...
- 使用inno setup制作安装包
- mx51 IPU 透明处理
Freescale MX51平台的透明处理根据bpp(bits_per_pixel)不同,处理方式有所不同. 透明处理涉及到两个图层的合并,这个合并操作是MX51 IPU的DP(Display pro ...
- studio_svn
最新升级IDEA12到13版本,升级后发现IDEA中SVN无法正常使用,但文件夹下能够正常使用. 并且报错:svn: E204899: Cannot run program "svn&quo ...