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. android 渐变drawable

    渐变Drawable它是使用<gradient>的标记的形状Drawable定义子节点的定义. 每个梯度Drawable求至少要有一个startColor和endColor属性,而且支持一 ...

  2. eclipse android ndk 提示Type 'JNIEnv' could not be resolved 等信息解决办法

    新配置完eclipse c++ android ndk 环境后,导入项目提示以下信息 是由于没有将jni.h导入的缘故,而这个文件在ndk的目录下面.所以,参照以下步骤:Project Propert ...

  3. Javascript入门视频教程

    1,第一节 http://pan.baidu.com/play/video#video/path=%2F%E6%95%99%E5%AD%A61.mov&t=-1 2,第二节 http://pa ...

  4. 北京设计模式学习组bjdp.org第7次活动(2013.08.04)回顾会纪要

    时间:2013.08.04,9am-7pm 地点:北京龙泉寺(北京凤凰岭风景区内) 参加人数:北京龙泉寺信息中心(20人).北京设计模式学习组(9人) 活动要点: 1)寺院巡礼:义工师兄带领参观寺院. ...

  5. 【POJ3268】Silver Cow Party 最短

    意甲冠军:一群奶牛去的地方.去回,然后回去寻找最伟大值. 题解:两遍最短路,结束.邻接矩阵存边能够避免建反图. #include <cstdio> #include <cstring ...

  6. Android采访开发——2.通用Android基础笔试题

    注意finddreams博客: http://blog.csdn.net/finddreams/article/details/44219231 正值跳槽的热季.整理一下Android面试中最常考的笔 ...

  7. HDU 4085 Steiner树模板称号

    Dig The Wells Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  8. google面试题,男孩男女比例?

    Google面试题: 在一个重男轻女的国家里,每一个家庭都想生男孩.假设他们生的孩子是女孩.就再生一个,直到生下的是男孩为止,这种国家.男女比例会是多少? 答案:1:1 分析:  出生男女概率是50% ...

  9. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(18)-过滤器的使用和批量删除数据(伪删除和直接删除)

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(18)-过滤器的使用和批量删除数据(伪删除和直接删除) ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   ...

  10. Android有效的治疗方法Bitmap,减少内存

    Android有效的治疗方法Bitmap,减少内存 照片可能有不同的大小. 在很多情况下,大小.比如,我们的Camera应用,我们所拍的照片的大小远大于屏幕显示的大小 假如你的应用被限制了内存使用,显 ...