152. 乘积最大子数组
2021-01-18 13:16
标签:target class pre rev tco subarray 链接 div ref 题目链接:https://leetcode-cn.com/problems/maximum-product-subarray/solution/ 作者:hyj8 152. 乘积最大子数组 标签:target class pre rev tco subarray 链接 div ref 原文地址:https://www.cnblogs.com/rtyxxy/p/12913117.htmlclass Solution {
public:
int maxProduct(vectorint>& nums) {
int len = nums.size(), res = nums[0];
int prevMin = nums[0], prevMax = nums[0];
int temp1 = 0, temp2 = 0;
for (int i = 1; i ) {
temp1 = prevMin * nums[i];
temp2 = prevMax * nums[i];
prevMin = min(min(temp1, temp2), nums[i]);
prevMax = max(max(temp1, temp2), nums[i]);
res = max(prevMax, res);
}
return res;
}
};
链接:https://leetcode-cn.com/problems/maximum-product-subarray/solution/wa-ni-zhe-ti-jie-shi-xie-gei-bu-hui-dai-ma-de-nu-p/
来源:力扣(LeetCode)
感谢!