您的位置:首页 > 其它

[LeetCode] Maximum Product Subarray

2014-10-22 12:05 295 查看
找出最大产品量

Find the contiguous subarray within an array (containing at least one number) which has the largest product.
https://oj.leetcode.com/problems/maximum-product-subarray/
For example, given the array
[2,3,-2,4]
,
the contiguous subarray
[2,3]
has the largest product =
6
.

啊啊啊。做不出来`~~~~

1. 最笨的解答办法

public class Solution {
public int maxProduct(int[] A) {
if( A.length<1) return 0;
int sum = A[0];
int cur = A[0];

for( int i=1; i<A.length ; i++) {
sum =
sum = maxProductByN( A , i );
}

return sum;
}

public int maxProductByN(int[] A ,int n) {
if( A.length<1) return 0;
int sum = A[0];
int cur = A[0];

for(int j=0;j<A.length-n;j++){
for( int i=j; i<j+n; i++) {
cur *= A[i];
}
sum = sum < cur ? cur : sum;
}
return sum;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: