给你一个整数数组 nums ,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。
测试用例的答案是一个 32-位 整数。
子数组 是数组的连续子序列。
-------------------------------------------------------------------------------------------------------------------------
思路:动态规划 存住下标0得知 遍历数组从一开始
就三种状态 前面的乘到当前最大 当前最大 负数的情况
所以需要一个值存最小的 最小值为正数对于最大值判断没用 但为负数就另说了
class Solution {
public int maxProduct(int[] nums) {
int maxf=nums[0],minf=nums[0],res=nums[0];
for(int i=1;i<nums.length;i++){
int mx=maxf,mn=minf;
maxf=Math.max(mx*nums[i],Math.max(nums[i],mn*nums[i]));
minf=Math.min(mn*nums[i],Math.min(nums[i],mx*nums[i]));
res=Math.max(res,maxf);
}
return res;
}
}