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. C++-Effective C++ Items

    Item2:尽量以const,enum,inline替换#define 原因:1, #define ASPECT_RATIO 1.63 编译错误时产生魔数,应以const double Aspect_ ...

  2. Rhel7的基本使用

    1.修改主机名 [root@localhost ~]# cat /etc/hostname localhost.localdomain[root@localhost ~]# hostnamectl s ...

  3. mysql 启动错误1026

    进入“事件查看器”“应用程序”果然发现很多MySql的错误Default storage engine (InnoDB) is not available 于是进入MySql的安装目录找到my.ini ...

  4. HDU 1203-Program D

    Description Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材料,于是,便需要去申请学校了.要申请国外的任何大学,你都要交纳一定的申请费用,这可是很惊人的 ...

  5. Visual Studio安装过程

    在这里需要先跟老师说一声抱歉,因为编写代码的愿意,我早在大一的时候就已经安装并且购买了正版的VS2013.所以今天在这里实在无法全部描述VS2013的安装过程. 然而,我所知的是,VS2013相对于我 ...

  6. hibernate实现增删改查的各种方法

    1>接口(主要是增删改查的接口)BaseDao.java /** * * @author fly.zhou */ public interface IBaseDao { //增加对应实体的一条记 ...

  7. Android Context

    http://www.cnblogs.com/android100/p/Android-Context.html

  8. hdu 2080

    ps:水题...求夹角...先求出COS,然后用acos 代码: #include "stdio.h" #include "math.h" int main() ...

  9. 【Leetcode】 LRU Cache实现

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  10. Svn win7系统下状态图标不显示-转载

    Svn win7系统下状态图标不显示 Svn版本 tortoisesvn-1.8.8.25755-x64-svn-1.8.10.msi 2.不显示图标状态如图1,期望结果显示图标状态如图2 图1 图2 ...