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. Java:String、StringBuffer和StringBuilder的区别

    1 String String:字符串常量,字符串长度不可变.Java中String是immutable(不可变)的. String类的包含如下定义: /** The value is used fo ...

  2. WP8 独立存储 总结3(应用设置)

    •可在独立存储中使用ApplicationSettings对象•在独立存储中存储键/值对的Dictionary方式存储 •存储的对象将永久保存 在应用设置中保存数据 void saveString(s ...

  3. MyEclipse8.5集成Tomcat7

    我最近需要在MyEclipse中使用Tomcat7,已经在Servers中配置了本地的Tomcat路径,之后发布项,在MyEclipse启动Tomcat服务则出现如下错误提示: Exception i ...

  4. 读者写者问题继 读写锁SRWLock

    在<秒杀多线程第十一篇读者写者问题>文章中我们使用事件和一个记录读者个数的变量来解决读者写者问题.问题虽然得到了解决,但代码有点复杂.本篇将介绍一种新方法--读写锁SRWLock来解决这一 ...

  5. JavaScript:变量对象(Variable Object)

    引言:在使用JavaScript编程的时候,避免不了声明函数和变量,但是我们很少知道解释器是如何并且在什么地方找到这些函数和变量的,我们在引用这些对象的时候究竟发生了什么? 对ECMAScript程序 ...

  6. centos7的网络配置以及设置主机名和绑定IP的问题

    CentOS 7.0系统是一个很新的版本哦,很多朋友都不知道CentOS 7.0系统是怎么去安装配置的哦,因为centos7.0与以前版本是有很大的改进哦. 说明:截止目前CentOS 7.x最新版本 ...

  7. poj2184 背包

    //Accepted 1492 KB 110 ms //背包 //把si看成weight,Fi看成value,这可以表示成当dp[j]=max(dp[j-weight[i]]+value[i]) // ...

  8. cf--1C

    //Accepted 0 KB 60 ms //给出正多变形上的三个点,求正多形的最小面积 //记三个点之间的距离a,b,c; //由余弦定理得cosA //从而可求出sinA,和正多边形所在外接圆的 ...

  9. Oracle的DDL、DML、DCL

    DDL (Data Definition Language 数据定义语言) create table 创建表 alter table 修改表 drop table 删除表 truncate table ...

  10. C++数据结构之Queue(队列)

    Queue,队列,和我们日常生活中的队列是同样的规则,"先进先出",从尾入,从首出. Queue,主要有三种基本操作,append(添加元素至队尾):serve(队首元素出列):r ...