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的更多相关文章

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

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

  2. [LeetCode]152. Maximum Product Subarray

    This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...

  3. 【LeetCode】152. Maximum Product Subarray 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 动态规划 参考资料 日期 题目地址:htt ...

  4. 【LeetCode】152. Maximum Product Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  5. LeetCode OJ:Maximum Product Subarray(子数组最大乘积)

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

  6. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  7. 152. Maximum Product Subarray - LeetCode

    Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...

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

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

  9. [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

随机推荐

  1. [JS]省市区数据及方法调用

    调用方法: function GetProvinceByid(id) { if (id == null || id == undefined || id == "") return ...

  2. OllyDBG V1.10聆风听雨汉化版

    软件名称:OllyDBG V1.10聆风听雨汉化版 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 3.84MB 图片预览: 软件简介: Ollydbg2. ...

  3. 改造jQuery-Tagit 插件支持中文全角的逗号和空格

    jQuery 的 tagit 插件效果还是不错的,今天用到该插件但发现不能自定义标签分隔符,只能是英文半角逗号或空格,于是想改造下 效果: 先研究了一番插件的代码,发现并不能通过插件自身的扩展方法来实 ...

  4. 【2】JavaScript编程全解笔记(二)

    你过去的种种经历,就像人生的一颗颗珍珠,在未来的某一天,你找到了那根线,你就会把她们串联起来,变成美丽的项链. 第八章 客户端 JavaScript 与 HTML 1. 浏览器渲染页面的步骤 2.   ...

  5. excel 常用函数

    1.统计整列唯一值的数量 =sumproduct(1/countif(offset(A1,,,COUNTA(A:A)),OFFSET(A1,,,COUNTA(A:A))))

  6. iOS缓存

    存储缓存: 第三方应用只能把信息保存在应用程序的沙盒中.因为缓存数据不是用户产生的,所以它应该被保存在NSCachesDirectory,而不是NSDocumentsDirectory.为缓存数据创建 ...

  7. Hbase压力测试

    PerformanceEvaluation是HBase自带的性能测试工具,该工具提供了顺序读写.随机读写.扫描等性能测试功能.本文简要介绍HBase PerformanceEvaluation的使用方 ...

  8. fedora22 mysql安装

    fedora19以后好像取消了对mysql的支持,看其他人好像说是用的mariadb的.centos里用yum安装的方式,放到fedora中不能用,所以找了很多资料,尝试了一种可行的办法. 在Fedo ...

  9. 用ftplib爆破FTP口令

    #coding:utf-8 #author:jwong import ftplib def bruteLogin(hostname,passwordFile): with open(passwordF ...

  10. MVC视图路径修改方法

    http://wenku.baidu.com/link?url=MwAaKgGevU7hfRuTyCL95ZbJuDsNc4b__jEWisY9GuzAJzEUgEdoj7uQ-wurbYtz1IQj ...