Leetcode no. 152
2016-05-29 19:09
260 查看
152. Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
For example, given the array
the contiguous subarray
public class Solution {
public int maxProduct(int[] nums) {
if (nums.length==0) return 0;
int max= nums[0];
int prevmax= nums[0];
int prevmin= nums[0];
int curmax, curmin;
for (int i=1; i<nums.length; i++) {
curmax= Math.max(Math.max(prevmax*nums[i], prevmin*nums[i]), nums[i]);
curmin= Math.min(Math.min(prevmax*nums[i], prevmin*nums[i]), nums[i]);
max= Math.max(max, curmax);
prevmax= curmax;
prevmin= curmin;
}
return max;
}
}
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) {
if (nums.length==0) return 0;
int max= nums[0];
int prevmax= nums[0];
int prevmin= nums[0];
int curmax, curmin;
for (int i=1; i<nums.length; i++) {
curmax= Math.max(Math.max(prevmax*nums[i], prevmin*nums[i]), nums[i]);
curmin= Math.min(Math.min(prevmax*nums[i], prevmin*nums[i]), nums[i]);
max= Math.max(max, curmax);
prevmax= curmax;
prevmin= curmin;
}
return max;
}
}
相关文章推荐
- 中断处理程序中输出调试信息导致无法计算机无法启动
- BLE 连接间隔最大值和最小值的问题
- 在Ubuntu Server14.04上编译Android6.0源码
- SQL 函数
- node中的path模块
- 基于物理渲染的理论基础
- Unix & Linux笔记(17)
- Java compiler level does not match the version of the installed Java project facet.
- 深入理解Java:SimpleDateFormat安全的时间格式化
- QDataStream和QByteArray
- android intent的跳转,页面传值
- NYOJ 挑战密室---1236第八届
- 写时拷贝机制(copy on write)(转自搜狐达人空间的笔记)
- WPF的一些总是记不住的Tips
- 基于多线程多用户的FTP服务器与客户端功能实现
- poj 1190 dfs+剪枝
- 连载shell(一): 交互式脚本,随日期变化,数值运算
- [转载]python实现带验证码网站的自动登陆
- 线程的五大状态
- MessageBox会阻塞信号槽