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: ...
随机推荐
- Java注解配置
Java注解是附加在代码中的一些元信息,用于一些工具在编译.运行时进行解析和使用,起到说明.配置的功能.注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用.包含在 java.lang.annota ...
- 获取Android系统的版本号
int currentVersion = android.os.Build.VERSION.SDK_INT;
- Ohlàlà
Chap 1数数字 un 1 deux 2 trois 3 quatre 4 cinq 5 six 6 sept 7 huit 8 neuf 9 dix 10 Chap 2 讲地名 Paris 巴 ...
- 在ubunut下使用pycharm和eclipse进行python远程调试
我比较喜欢Pycharm,因为这个是JetBrains公司出的python IDE工具,该公司下的java IDE工具--IDEA,无论从界面还是操作上都甩eclipse几条街,但项目组里有些人使用e ...
- objectARX判断当前坐标系
判断当前坐标系是WCS还是UCS 使用系统变量 WORLDUCS 请参见 用户坐标系 (UCS) 概述 (只读) 类型: 整数 保存位置: 未保存 初始值: 1 指示 UCS 是否与 WCS 相同 ...
- fwrite错误
使用fwrite出错 f:\dd\vctools\crt_bld\self_x86\crt\srt\write.cline:69expression:_osfile(fh)&FOPEN 使用w ...
- 关于WinForm引用WPF窗体---在Winform窗体中使用WPF控件
项目中有个界面展示用WPF实现起来比较简单,并且能提供更酷炫的效果,但是在WinForm中使用WPF窗体出现了问题,在网上找了一下有些人说Winform不能引用WPF的窗体,我就很纳闷,Win32都能 ...
- linux下IPTABLES配置详解
如果你的IPTABLES基础知识还不了解,建议先去看看. 开始配置 我们来配置一个filter表的防火墙. (1)查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables - ...
- VM虚拟机中Ubuntu无法连接网络
之前Ubuntu是可以上网的,但是今天打开后发现上不了网了,经过一番折腾,弄好了,记录下. 方案一:将网络连接设置为自定义NAT VM ->设置-> 硬件->网络适配器 这么已修改就 ...
- ios工程中ARC与非ARC的混合
ARC与非ARC在一个项目中同时使用, 1,选择项目中的Targets,选中你所要操作的Target,2,选Build Phases,在其中Complie Sources中选择需要ARC的文件双击,并 ...