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. 一张图让你看清Java集合类(Java集合类的总结)

    如今关于Java集合类的文章非常多,可是我近期看到一个非常有意思图片,基本上把Java集合的整体框架都给展现出来了.非常直观. watermark/2/text/aHR0cDovL2Jsb2cuY3N ...

  2. socket抓取网页

    #include <iostream> #include <string> #include <netdb.h> #include <stdio.h> ...

  3. NSIS:判断程序是否运行并进行卸载

    原文NSIS:判断程序是否运行并进行卸载 今天在评论里看到网友说要一个这样的功能,就简单写了一个,本来想做360杀手来着,但遗憾的是我从来不用360的东西,所在电脑上也没有360相关的软件进行测试,所 ...

  4. 开展project 正常的生活之路

    相对刚走出学校的学生在其他行业工作,竞争力的薪酬,同时.并不断地不仅学习更新专业知识让你感到生活的充实,更满足了你那不让外人知的虚荣心.在刚出校门的几年中,你常常回头看看被你落在后面的同学们,在内心怜 ...

  5. LevelDB初体验

    近期工作须要找一个能使用磁盘存储数据,对写要求比較苛刻,须要每秒达100000TPS,读的时候须要能10000TPS左右,不能占用太多内存.单节点满足这个要求的常见有Redis.Memcached等, ...

  6. hdu 亲和串(kmp)

    Problem Description 人随着岁数的增长是越大越聪明还是越大越笨,这是一个值得全世界科学家思考的问题,同样的问题Eddy也一直在思考,因为他在很小的时候就知道亲和串如何判断了,但是发现 ...

  7. Leetcode:convert_sorted_array_to_binary_search_tree

    一.     称号 排序后的数组成二叉搜索树. 二.     分析 BST的中序遍历是一个sorted-array,再构造回去成一个BST,先将中间的元素作为根节点,这个节点的左右各自是左子树和右子树 ...

  8. Quartz.Net任务统一调度框架

    山寨版Quartz.Net任务统一调度框架   TaskScheduler 在日常工作中,大家都会经常遇到Win服务,在我工作的这些年中一直在使用Quartz.Net这个任务统一调度框架,也非常好用, ...

  9. 项目中经常使用的JS方法汇总,非常有用

    // 对Date的扩展,将 Date 转化为指定格式的String   // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符,   // 年(y)可以用 1- ...

  10. java-新浪微博开放平台——话题跟踪

    代码 网盘地址:http://pan.baidu.com/s/1pJ1D0Kz