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.

找出最大的相乘的数字,很简单,代码还可以优化的地方很多。但是速度还可以。

public class Solution {
public int maxProduct(int[] nums) {
int len = nums.length;
if( nums.length == 1)
return nums[0];
int[] result = new int[2];
int target = 0; int left_right = 0,right_left = 0;
for( int i = 0 ; i < len ; i ++){
if( nums[i] == 0){
target = Math.max(target,result[0]);
result[0] = 0;
result[1] = 0;
target = 0;
}else if( nums[i] > 0 ){
if( result[0] != 0)
result[0] *= nums[i];
else
result[0] = nums[i];
}else if( nums[i] < 0 ){
if( result[1] == 0 ){
result[1] = nums[i]*(result[0] == 0?1:result[0]);
result[0] = 0;
}
else{
if( result[0] == 0){
result[0] = result[1]*nums[i];
result[1] = 0;
}else{
result[0] = result[0]*result[1]*nums[i];
result[1] = 0;
}
}
}
left_right = Math.max(Math.max(result[0],target),left_right); } target = 0;
result[0] = 0;
result[1] = 0;
for( int i = len-1;i>=0;i--){
if( nums[i] == 0){
target = Math.max(target,result[0]);
result[0] = 0;
result[1] = 0;
target = 0;
}else if( nums[i] > 0 ){
if( result[0] != 0)
result[0] *= nums[i];
else
result[0] = nums[i];
}else if( nums[i] < 0 ){
if( result[1] == 0 ){
result[1] = nums[i]*(result[0] == 0?1:result[0]);
result[0] = 0;
}
else{
if( result[0] == 0){
result[0] = result[1]*nums[i];
result[1] = 0;
}else{
result[0] = result[0]*result[1]*nums[i];
result[1] = 0;
}
}
}
right_left = Math.max(Math.max(result[0],target),right_left);
} return Math.max(left_right,right_left);
}
}

leetcode 152. Maximum Product Subarray --------- java的更多相关文章

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

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

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

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

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

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

  4. Java for LeetCode 152 Maximum Product Subarray

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

  5. C#解leetcode 152. Maximum Product Subarray

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

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

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

  7. Leetcode#152 Maximum Product Subarray

    原题地址 简单动态规划,跟最大子串和类似. 一维状态空间可以经过压缩变成常数空间. 代码: int maxProduct(int A[], int n) { ) ; ]; ]; ]; ; i > ...

  8. 152. Maximum Product Subarray - LeetCode

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

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

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

随机推荐

  1. Zooming MKMapView to fit annotation pins

    http://stackoverflow.com/questions/4680649/zooming-mkmapview-to-fit-annotation-pins - (MKCoordinateR ...

  2. ANT-build.xml编译文件详解

    Ant 开发Ant的构建文件当开始一个新的项目时,首先应该编写Ant构建文件.构建文件定义了构建过程,并被团队开发中每个人使用.Ant构建文件默认命名为build.xml,也可以取其他的名字.只不过在 ...

  3. FR报表 自动缩小的代码

    procedure TfrMemoView.Draw(Canvas: TCanvas); var newdx: Integer; OldScaleX, OldScaleY: Double; fs: i ...

  4. Struts2 的验证

    概述 一个健壮的 web 应用程序必须确保用户输入是合法.有效的. Struts2 的输入验证 –基于 XWork Validation Framework 的声明式验证:Struts2 提供了一些基 ...

  5. 网络安全之PHP安全编程建议

    要提供互联网服务,当你在开发代码的时候必须时刻保持安全意识.可能大部分 PHP 脚本都对安全问题都不在意,这很大程度上是因为有大量的 无经验程序员 在使用这门语言.但是,没有理由让你因为对你的代码的不 ...

  6. 面向对象之prototype,__proto__

    var person = function(name) { this.name = name }; person.prototype.getName = function() { return thi ...

  7. operation not possible due to RF-kill

    使用mdk3时出现这个问题operation not possible due to RF-kill 就是输入第一条命令 后出现 operation not possible due to RF-ki ...

  8. <button>使用注意问题

    最近在项目的上传功能下(IE8)发现了如下的错误: 2015-08-13 09:14:03,396 WARN   [WARN] [http-8080-5] : Handler execution re ...

  9. C++的三种继承方式简述

    C++对父类(也称基类)的继承有三种方式,分别为:public继承.protected继承.private继承.三种继承方式的不同在于继承之后子类的成员函数的"可继承性质". 在说 ...

  10. MagSpoof:能预测并窃取你下一张信用卡号码的廉价设备

    想象一下,你丢失了信用卡,并从银行申请了一张新的信用卡.但是,如果在你收到这张新卡之前,一些网络罪犯就已经在使用你的新信用卡,此时你作何感想?是的,这完全是可以实现的,至少使用这个仅仅10美元的设备M ...