找出一个序列中乘积最大的连续子序列(至少包含一个数)。
样例
比如, 序列 [2,3,-2,4]
中乘积最大的子序列为 [2,3]
,其乘积为6
。
public class Solution {
/**
* @param nums: an array of integers
* @return: an integer
*/
public int maxProduct(int[] nums) {
// write your code here
if(nums==null||nums.length<1){
return 0;
}
int max=Integer.MIN_VALUE;
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*=nums[j];
}
max=Math.max(max, temp);
}
return max;
}
}