public int maxProduct(int[] nums) {
int res = nums[0];
for (int i = 1, imax = nums[0], imin = nums[0]; i < nums.length; i++) {
if (nums[i] < 0) {
int tmp = imax;
imax = imin;
imin = tmp;
}
imax = Math.max(nums[i], imax * nums[i]);
imin = Math.min(nums[i], imin * nums[i]);
res = Math.max(res, imax);
}
return res;
}
152. Maximum Product Subarray
最新推荐文章于 2020-09-11 22:15:13 发布