1.(原文)问题描述

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.翻译

找最大的数组中的子数组乘积

 例如,给予的数组是[2,3,-2,4]
连续的字数组[2,3]有最大的乘积=6

3.解决思路分析

  肯定可以通过其他方式实现就是不断的循环遍历,可是这样的代价太大,时间复杂度很高,我们j可以转换下思路好好想想如何最高效的来解决这个问题。

既然是求最大乘积那么我们需要考虑的就是最大乘积出现的情况可能有哪些:很显然有两种,第一种就是最大的累积乘以一个正数那么我们获取了更大的值,

还有就是一个最小的累积的值乘以一个负数,那么也可能获取的是最大的值,因为我们只需要保存乘积的最大和最小值。我们可以把数组的第一个值作为

最大最小值的逻辑值来处理,然后进行下去;如果之前的最大和最小值同当前元素相乘之后,没有当前元素大(或小)那么当前元素就可作为新的起点。

4.实现过程

Solution.java

package MaximumSubArray;

public class Solution {

	 public int maxProduct(int[] A) {

		 if(A == null||A.length==0){
return 0;
}
int maxProduct = A[0]; //最大值的初始值
int maxTemp = A[0]; //累积的最大值
int minTemp = A[0]; //累积的最小值 for(int i = 1;i < A.length;i++) {//从数组的第二个元素开始遍历 int a = A[i]*maxTemp;
int b = A[i]*minTemp;
maxTemp = Math.max(Math.max(a,b), A[i]);//选取最大值的新起点
minTemp = Math.min(Math.min(a,b), A[i]);//选取最小值的新起点
maxProduct = Math.max(maxProduct, maxTemp);//更新最大值
}
return maxProduct;
} }

 SolutionTest.java

package MaximumSubArray;

public class SolutionTest {

	/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub int []array1 = {0,2,3,5,6,8,0,9};
int []array2 = {1,-2,-3,-5,6,8,0,9};
int []array3 = {1,2,-3,5,6,8,0,9};
int []array4 = {1,2,0,5,6,8,0,9}; System.out.println(new Solution().maxProduct(array1));
System.out.println(new Solution().maxProduct(array2));
System.out.println(new Solution().maxProduct(array3));
System.out.println(new Solution().maxProduct(array4));
} }

 5.有句话很正确,解决思路很重要,重要的是思想。

LeetCode之Maximum Product Subarray的更多相关文章

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

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

  2. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

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

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

  4. LeetCode 152. Maximum Product Subarray (最大乘积子数组)

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

  5. [leetCode][001] Maximum Product Subarray

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

  6. Java for LeetCode 152 Maximum Product Subarray

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

  7. leetcode 152. Maximum Product Subarray --------- java

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

  8. C#解leetcode 152. Maximum Product Subarray

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

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

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

随机推荐

  1. GPU 编程入门到精通(五)之 GPU 程序优化进阶

    博主因为工作其中的须要,開始学习 GPU 上面的编程,主要涉及到的是基于 GPU 的深度学习方面的知识.鉴于之前没有接触过 GPU 编程.因此在这里特地学习一下 GPU 上面的编程. 有志同道合的小伙 ...

  2. python字典构造函数dict(mapping)解析

    Python字典的构造函数有三个,dict().dict(**args).dict(mapping),当中第一个.第二个构造函数比較好理解也比較easy使用, 而dict(mapping)这个构造函数 ...

  3. Cocos2d-x3.0游戏实例《不要救我》第一章——前言

    我们可以学习? 这是一个非常easy游戏.但更多的东西用(对于初学者).至少,对于它的一个例子,有点多. 笨木头花心贡献.啥?花心?不呢.是用心~ 转载请注明,原文地址:http://www.benm ...

  4. lsblk请参阅块设备

    lsblk可以查看分区和挂载的磁盘使用情况 lsblk全部的參数 -a, --all            显示全部设备  -b, --bytes          以bytes方式显示设备大小  - ...

  5. PC结束 Spark 二次开发 收到自己主动,并允许好友请求

    本次Spark二次开发是为了客服模块的开发, 能让用户一旦点击该客服则直接自己主动加入好友.而客服放则需自己主动加入好友,不同弹出对话框进行允许,这方便的广大客服. 如今废话不多说,直接上代码. pa ...

  6. Codeforces Round #274 (Div. 2) B. Towers

    As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting o ...

  7. 【iOS开展-50】使用它来创建一个新的类的实现代码包,因此,不自觉地练习简单MVC实验,附带动画

    接下来说说代码封装最后一个个案. 最后一种情况看:[iOS开展-48]九宫格案例:自己主动布局.字典转模型运用.id和instancetype差别.xib反复视图运用及与nib关系 (1)代码封装的原 ...

  8. SQL 注意事项

    -------选择表名 配置Ctrl+3 能够select * 桌 USE [NB] GO /* 物: StoredProcedure [dbo].[SP_Select] 脚本日期: 05/28/20 ...

  9. React学习系列

    React学习系列 系列学习react 翻译地址 https://scotch.io/tutorials/learning-react-getting-started-and-concepts 我是初 ...

  10. 值得关注的10个python语言博客(转)

    大家好,还记得我当时学习python的时候,我一直努力地寻找关于python的博客,但我发现它们的数量很少.这也是我建立这个博客的原因,向大家分享我自己学到的新知识.今天我向大家推荐10个值得我们关注 ...