給你一個整數數組 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;}
}