标签:leetcode解题报告 算法 面试题 java
LeetCode 新题又更新了,最大子数组乘积public class Solution { public int maxProduct(int A[]) { if(A==null||A.length==0) { return 0; } int[][] productArray = new int[A.length][A.length]; int maxProduct = A[0]; for(int i=0;i<A.length;i++) { for(int j=i;j<A.length;j++) { if(j==i) { productArray[i][i] = A[i]; } else { productArray[i][j] = productArray[i][j-1]*A[j]; } if(productArray[i][j]>maxProduct) { maxProduct = productArray[i][i]; } } } return maxProduct; } }
public class Solution { public int maxProduct(int A[]) { if(A==null||A.length==0) { return 0; } int maxProduct = A[0]; int max_temp = A[0]; int min_temp = A[0]; for(int i=1;i<A.length;i++) { int a = A[i]*max_temp; int b = A[i]*min_temp; max_temp = Math.max(Math.max(a,b), A[i]); min_temp = Math.min(Math.min(a,b), A[i]); maxProduct = Math.max(maxProduct, max_temp); } return maxProduct; } }
LeetCode Maximum Product Subarray 解题报告
标签:leetcode解题报告 算法 面试题 java
原文地址:http://blog.csdn.net/worldwindjp/article/details/39826823