题解 | #连续子数组的最大乘积#
连续子数组的最大乘积
http://www.nowcoder.com/practice/abbec6a3779940aab2cc564b22d36859
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @return int整型 */ int maxProduct(vector<int>& nums) { int res = INT_MIN; int mx = 1; int mn = 1; for(int i=0; i<nums.size(); i++){ if(nums[i] < 0){ int temp = mx; mx = mn; mn = temp; } mx = max(nums[i], mx * nums[i]); mn = min(nums[i], mn * nums[i]); res = max(res, mx); } return res; // write code here } };
https://leetcode-cn.com/problems/maximum-product-subarray/solution/hua-jie-suan-fa-152-cheng-ji-zui-da-zi-xu-lie-by-g/