152. Maximum Product Subarray
Problem
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.
Example 1:
Input: [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.
Example 2:
Input: [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
Code
class Solution {
public int maxProduct(int[] nums) {
if(nums == null || nums.length == 0) return 0;
int max = nums[0];
int min = nums[0];
int res = nums[0];
for(int i = 1; i < nums.length; i++){
int temp = max;
max = Math.max(Math.max(max * nums[i], min * nums[i]), nums[i]);
min = Math.min(Math.min(min * nums[i], temp * nums[i]), nums[i]);
res = Math.max(max, res);
}
return res;
}
}
按 <- 键看上一题!
151. Reverse Words in a String
按 -> 键看下一题!
153. Find Minimum in Rotated Sorted Array