[Lintcode]Maximum Product Subarray乘积最大子序列
来源:互联网 发布:js ajax获取网页 编辑:程序博客网 时间:2024/06/06 12:02
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
Example
For example, given the array [2,3,-2,4]
, the contiguous subarray [2,3]
has the largest product = 6
.
public class Solution { /** * @param nums: an array of integers * @return: an integer */ public int maxProduct(int[] nums) { if(nums.length == 0) return 0; int[][] res = new int[nums.length][nums.length]; int max = Integer.MIN_VALUE; for(int i = 0; i < nums.length; i++) { res[i][i] = nums[i]; max = Math.max(max, res[i][i]); for(int j = i + 1; j < nums.length; j++) { res[i][j] = res[i][j - 1] * nums[j]; max = Math.max(max, res[i][j]); } } return max; }}
看了一下,二维数组多余。直接用一个int代替。通过。
public class Solution { /** * @param nums: an array of integers * @return: an integer */ public int maxProduct(int[] nums) { if(nums.length == 0) return 0; int max = Integer.MIN_VALUE; for(int i = 0; i < nums.length; i++) { int start = nums[i]; max = Math.max(max, start); for(int j = i + 1; j < nums.length; j++) { start = start * nums[j]; max = Math.max(max, start); } } return max; }}
0 0
- [Lintcode]Maximum Product Subarray乘积最大子序列
- [leetcode]Maximum Product Subarray最大子序列乘积
- [LeetCode]Maximum Product Subarray 新题152 最大乘积子序列
- 191.Maximum Product Subarray-乘积最大子序列(中等题)
- Leetcode 152 Maximum Product Subarray 子数组最大乘积
- LeetCode Maximum Product Subarray(最大子数组乘积)
- leetcode152-Maximum Product Subarray(乘积最大子数组)
- Maximum Product Subarray 乘积最大的子数组
- LeetCode:152. Maximum Product Subarray Java 子数组最大乘积
- 【LeetCode】152. Maximum Product Subarray最大连续子数组乘积
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
- Array-----152. Maximum Product Subarray(最大连续子数组乘积)
- LeetCode 152. Maximum Product Subarray--连续子数组最大乘积
- 152. Maximum Product Subarray (最大乘积子数列)
- Maximum Subarray Maximum Product Subarray 子数组的最大和 最大乘积
- LintCode-乘积最大子序列
- LintCode -- 乘积最大子序列
- lintcode- 乘积最大子序列
- JSP 用户提交请求
- vaadin插件安装方法eclipse
- LoadLayout快速实现加载中界面,加载错误界面,空界面等功能
- 多线程之synchronized(this)
- 并查集(DSU)
- [Lintcode]Maximum Product Subarray乘积最大子序列
- 音频(四) - 音频压缩(Speex使用&Opus简介)
- DataGridView第一行加序号
- AngularJS2.0 入门教程
- Leetcode 401 Binary Watch
- android在onCreate()方法中获取View的宽度与高度的方法实战
- 容易混淆的器件——硬件
- SAP中MM中取classification值调用函数
- guava实现MD5加密