给你一个整数数组 nums ,请你找出数组中乘积最大的连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。
示例 1:
输入: [2,3,-2,4]
输出: 6
解释: 子数组 [2,3] 有最大乘积 6。
代码
class Solution {
public int maxProduct(int[] nums) {
int n=nums.length,res=Integer.MIN_VALUE;
int[] dp=new int[n];
for(int i=0;i<n;i++)//计算所有子数组的乘积
for(int j=i;j<n;j++)
{
if(i==j) dp[j]=nums[i];
else dp[j]=dp[j-1]*nums[j];
res= Math.max(res,dp[j]);
}
return res;
}
}