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 ...
随机推荐
- ECMAScript6之let与const关键字
let关键字 let关键字和var关键字一样,都是用来声明变量的,但是和var不同的是,let关键字声明的变量仅在自己的块级作用域范围内发挥作用. 我们来比较下面两段代码 var arr = new ...
- 使用pycharm+pyqt5 触发自定义方法
1.依旧使用上次QtDesigner做的界面,如下图: 2.本次的使用text Browser 和 text Edit 和 pushButton控件做触发联系: 3.目的实现在text Edit中随意 ...
- zabbix 布署实践【6 使用微信公众号-消息模版推送告警】
使用这个服务的前提是,你必须要有一个微信订阅号,或者公众号,并且是通过认证的号 因为认证过后的号才有模版消息和获取用户openid等信息的权限 ,如下,登录微信公众号的登录页后,底下有个接口权限的展示 ...
- metasploit nessus & db_autopwn
nessus官网:https://www.tenable.com/products/nessus-vulnerability-scanner 下载地址:https://www.tenable.com/ ...
- Python学习笔记——基础篇【第五周】——random & time & datetime模块
random模块 随机数 mport random print random.random() print random.randint(1,2) print random.randrange(1,1 ...
- java 缓存ehcache的使用(使用方式一)
实体要序列化 resource文件夹下建立 ehcache.xml <?xml version="1.0" encoding="UTF-8"?> & ...
- Python基础(七)-文件操作
一.文件处理流程 1.打开文件,得到文件句柄赋值给一个变量 2.通过句柄对文件进行操作 3.关闭文件 二.基本操作 f = open('zhuoge.txt') #打开文件 first_line = ...
- 一个初学者的辛酸路程-python操作SQLAlchemy-13
前言 其实一开始写blog,我是拒绝的,但是,没办法,没有任何理由抗拒.今天呢,要说的就是如何使用Python来操作数据库. SQLAlchemy SQLAlchemy是Python编程语言下的一款O ...
- es6--(二)变量的解构赋值
1.数组的解构赋值 //数组解析 let [a,b,c] = [1,2,3]; //a=1;b=2;c=3 //嵌套数组 let [a,[b,c]] = [1,[2,3]];//a=1;b=2;c=3 ...
- Oracle Day 08 游标与例外的总结
1.游标的使用(cursor) 基本格式: 定义游标: cursor 游标名 is select语句; 打开游标: open 游标名; loop(循环) fetch ... into ...; ...