# 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.

 1 public class Solution {
2     public int maxProduct(int[] A) {
3         int n = A.length;
4         int mint = 1;
5         int maxt = 1;
6
7         int maxvalue = A[0];
8         for(int i =  0 ; i < n ; i++){
9             if(A[i] == 0){
10                 mint = 1;
11                 maxt = 1;
12                 if(maxvalue < 0)
13                     maxvalue = 0;
14             }else{
15                 int curmax = maxt * A[i];
16                 int curmin = mint * A[i];
17
18                 maxt = curmax > curmin ? curmax : curmin;
19                 mint = curmax > curmin ? curmin : curmax;
20
21                 if(maxt < A[i])
22                     maxt = A[i];
23
24                 if(mint > A[i])
25                     mint = A[i];
26
27                 if(maxt > maxvalue)
28                     maxvalue = maxt;
29             }
30         }
31
32         return maxvalue;
33
34     }
35 }

posted @ 2014-10-21 21:07  非著名程序师  阅读(178)  评论(0编辑  收藏