leetcode 152. Maximum Product Subarray --------- java
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的更多相关文章
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Java for LeetCode 152 Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- C#解leetcode 152. Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode]152. Maximum Product Subarray最大乘积子数组
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- Leetcode#152 Maximum Product Subarray
原题地址 简单动态规划,跟最大子串和类似. 一维状态空间可以经过压缩变成常数空间. 代码: int maxProduct(int A[], int n) { ) ; ]; ]; ]; ; i > ...
- 152. Maximum Product Subarray - LeetCode
Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
随机推荐
- 分布式一致性原理—BASE
定义 BASE是BasicallyAvailable(基本可用).Soft state(软状态)和Eventually consistent(最终一致性)三个短语的简写,是由来自eBay的架构师Dan ...
- [网络技术]网关 路由器 OSI
tracert 1.网关与路由 关键的区别:网关是这样一个网络节点:以两个不同协议搭建的网络可以通过它进行通信.路由器是这样一种设备:它能在计算机网络间收发数据包,同时创建一个覆盖网络(overlay ...
- C# SVN检出的代码,F12显示从元数据
解决办法: 删除项目中的引用(同时也要删除bin文件夹中的dll文件,否则不能重新添加),并重新添加本地引用即可. 原因: 项目中的dll文件不是本机编译出来的,所以找不到元数据.如果当前关联的项目里 ...
- What am I missing out in life if I don't have a girlfriend?
http://www.quora.com/What-am-I-missing-out-in-life-if-I-dont-have-a-girlfriend/answer/Kelly-Erickson ...
- GoldenGate 之 Bounded Recovery说明
首先,我们来看两个OGG同步中可能的问题: l oracle在线日志包含已提交的和未提交的事务,但OGG只会将已提交的事务写入到队列文件.因此,针对未提交的事务,特别是未提交的长事务,OGG会怎样处理 ...
- dotTracePerormance 工具
今天凌晨 阿根廷对瑞士比赛已经过去,比分是1:0 阿根廷获胜:虽说我是伪球迷,但是也挺希望梅西进入决赛.昨晚也压了下90分之内 0:0 ,结果胜出:另一场压的是美国对比利时,也是压平,就这样二串 ...
- Hadoop c++开发
假设你有上百G的数据,你要统计出这些数据中,含有某些你感兴趣的内容的数据的有多少条,你会怎么做?在硬件条件允许的情况下,用hadoop并行计算是一个不错的选择. 为了使本文得以清晰地说明,我们不妨假设 ...
- 《c语言全局变量的用法》
//全局变量的用法. /*有一个一维数组,内放n个学生的成绩,(n由用户自己指定,通过调用函数实现定义一个数组.)写一个函数,当主函数调用此函数后,能求出平均分,最高分,最低分.*/ #include ...
- Mousejack测试指南
0x00 前言 近日,Bastille的研究团队发现了一种针对蓝牙键盘鼠标的攻击,攻击者可以利用漏洞控制电脑操作,他们将此攻击命名为MouseJack. 攻击者仅需要在亚马逊上以60美元购买设备,改造 ...
- 让NSURLConnection在子线程中运行
可以有两个办法让NSURLConnection在子线程中运行,即将NSURLConnection加入到run loop或者NSOperationQueue中去运行. 前面提到可以将NSTimer手动加 ...