Java for LeetCode 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 [2,3,-2,4],
the contiguous subarray [2,3] has the largest product = 6.
解题思路:
计算连续的积最大,由于会有负数出现,因此需要用两个int表示包含nums[i]的最大值和最小值,然后res=Math.max(res, max)即可,JAVA实现如下:
public int maxProduct(int[] nums) {
if(nums.length==1)
return nums[0];
int max=nums[0],min=nums[0],res=nums[0],maxTemp=0,mintemp=0;
for(int i=1;i<nums.length;i++){
maxTemp=max*nums[i];
mintemp=min*nums[i];
max=Math.max(nums[i],Math.max(maxTemp, mintemp));
min=Math.min(nums[i],Math.min(maxTemp, mintemp));
res=Math.max(res, max);
}
return res;
}
Java for LeetCode 152 Maximum Product Subarray的更多相关文章
- 求连续最大子序列积 - 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 ...
- leetcode 152. Maximum Product Subarray --------- java
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: ...
随机推荐
- C 文件读写2
feof() int feof(FILE *stream); 在执行读文件操作时,如果遇到文件尾,则函数返回逻辑真(1):否则,则返回逻辑假(0). feof()函数同时适用于ASCII码文件和二进 ...
- 【codevs1014/1068】背包型动态规划
分析: 状态转移方程: v[j]=max(v[j],v[j-a[i]]+a[i]) (j ← tol downto a[i]) /* 作者:flipped 题目:p1014 装箱问题 */ #incl ...
- Mysql常出现的问题
1.mysql如何导入.txt文件?load data local infile 'D:\\data.txt' into table 表名 fields terminated by '\t';2.my ...
- DNA repair问题
问题:Biologists finally invent techniques of repairing DNA that contains segments causing kinds of inh ...
- 【bzoj2115】 Xor
www.lydsy.com/JudgeOnline/problem.php?id=2115 (题目链接) 题意 给出一张图,可能有重边和自环,在图中找出一条从1-n的路径,使得经过的路径的权值的异或和 ...
- Redis操作+python
自动化接口测试中需要向redis中插入测试数据: 1. 连接redis: import redisself.r = redis.StrictRedis(host=env.REDIS_HOST, por ...
- C#实现通过程序自动抓取远程Web网页信息的代码
http://www.jb51.net/article/9499.htm 通过程序自动的读取其它网站网页显示的信息,类似于爬虫程序.比方说我们有一个系统,要提取BaiDu网站上歌曲搜索排名.分析系统在 ...
- c++新特性与boost
<Boost程序库探秘——深度解析C++准标准库>之试读 前一阵子还看到一篇文章,说C#要重蹈C++的覆辙,这里说的C++的覆辙是什么呢?是指C++语言过于臃肿的功能特性,导致学习人员的流 ...
- Oracle数据分页,并传出数据集
1.创建Package create or replace package forPaged is type my_csr is ref cursor; procedure getPaged(tabl ...
- servlet运行流程
servlet运行流程 (2013-06-19 19:16:43) 转载▼ 首先Servlet被部署到Web容器中,当客户端发送调用这个Servlet的请求到达Web容器时,Web容器会先判 ...