题解 | #子数组最大乘积#
子数组最大乘积
http://www.nowcoder.com/practice/9c158345c867466293fc413cff570356
public class Solution {
public double maxProduct(double[] arr) {
if(arr.length==0) return 0;
double max = arr[0];
double min = arr[0];
double val = max;
double tmp = 1;
for(int i=1;i<arr.length;i++){
tmp = max; //此处要先把上一把的max保留一下
max = Math.max(arr[i],arr[i]*max);
max = Math.max(max,arr[i]*min);
min = Math.min(arr[i],arr[i]*min);
min = Math.min(min,arr[i]*tmp);
val = Math.max(val,max);
}
return val;
}
}