191 · 乘积最大子序列Maximum Product Subarray
描述
找出一个序列中乘积最大的连续子序列(至少包含一个数)。
样例 1:
输入:[2,3,-2,4]
输出:6
public class Solution {
/**
* @param nums: An array of integers
* @return: An integer
*/
public int maxProduct(int[] nums) {
// write your code here
int max = Integer.MIN_VALUE ;
int n = nums.length ;
for(int i = 0 ; i < nums.length ; i++){
int temp = nums[i] ;
for(int j = i+1 ; j < nums.length ; j++){
max = Math.max(max , temp) ;
temp = temp * nums[j] ;
}
max = Math.max(max , temp) ;
}
return max ;
}
}