题解 | #连续子数组的最大乘积#

连续子数组的最大乘积

http://www.nowcoder.com/practice/abbec6a3779940aab2cc564b22d36859



public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param nums int整型一维数组 
     * @return int整型
     */
    public int maxProduct (int[] nums) {
        // write code here
        int last_max = nums[0];  
        int last_min = nums[0];  
        int result = nums[0];  
        int cur_max = nums[0];  
        int cur_min = nums[0];  

        for(int i = 1; i < nums.length; i ++)  
        {  
            cur_max = Math.max(nums[i], Math.max(last_max * nums[i], last_min * nums[i]));  
            cur_min = Math.min(nums[i], Math.min(last_max * nums[i], last_min * nums[i]));  
            result = Math.max(result, cur_max);  
            last_max = cur_max;  
            last_min = cur_min;  
        }  
        return result;
    }
}
全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务