找出最大产品量
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; } }