码迷,mamicode.com
首页 > 编程语言 > 详细

找出数组中的连续最大乘积

时间:2018-07-22 16:56:28      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:solution   turn   ++   i++   for   结果   连续   示例   max   

给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数)。

示例 1:

输入: [2,3,-2,4]
输出: 6
解释: 子数组 [2,3] 有最大乘积 6。
示例 2:

输入: [-2,0,-1]
输出: 0
解释: 结果不能为 2, 因为 [-2,-1] 不是子数组。

class Solution {
public int maxProduct(int[] nums) {
int fmax1, fmax2;
fmax2 = nums[0];
for(int i=0; i<=nums.length-1; i++){
fmax1 = nums[i];
for(int j=i; j<=nums.length-1; j++){
if(j > i){
fmax1 *= nums[j];
}
if(fmax2 < fmax1){
fmax2 = fmax1;
}
}
}
return fmax2;
}
}

找出数组中的连续最大乘积

标签:solution   turn   ++   i++   for   结果   连续   示例   max   

原文地址:http://blog.51cto.com/13845370/2148350

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!