leetcode 152. Maximum Product Subarray --------- java

Find the contiguous subarray within an array (containing at least one number) which has the largest product.

For example, given the array [2,3,-2,4],
the contiguous subarray [2,3] has the largest product = 6.

找出最大的相乘的数字,很简单,代码还可以优化的地方很多。但是速度还可以。

public class Solution {
    public int maxProduct(int[] nums) {
        int len = nums.length;
        if( nums.length == 1)
            return nums[0];
        int[] result = new int[2];
        int target = 0;

        int left_right = 0,right_left = 0;
        for( int i = 0 ; i < len ; i ++){
            if( nums[i] == 0){
                target = Math.max(target,result[0]);
                result[0] = 0;
                result[1] = 0;
                target = 0;
            }else if( nums[i] > 0 ){
                if( result[0] != 0)
                    result[0] *= nums[i];
                else
                    result[0] = nums[i];
            }else if( nums[i] < 0 ){
                if( result[1] == 0 ){
                    result[1] = nums[i]*(result[0] == 0?1:result[0]);
                    result[0] = 0;
                }
                else{
                    if( result[0] == 0){
                        result[0] = result[1]*nums[i];
                        result[1] = 0;
                    }else{
                        result[0] = result[0]*result[1]*nums[i];
                        result[1] = 0;
                    }
                }
            }
            left_right = Math.max(Math.max(result[0],target),left_right);

        }

        target = 0;
        result[0] = 0;
        result[1] = 0;
        for( int i = len-1;i>=0;i--){
            if( nums[i] == 0){
                target = Math.max(target,result[0]);
                result[0] = 0;
                result[1] = 0;
                target = 0;
            }else if( nums[i] > 0 ){
                if( result[0] != 0)
                    result[0] *= nums[i];
                else
                    result[0] = nums[i];
            }else if( nums[i] < 0 ){
                if( result[1] == 0 ){
                    result[1] = nums[i]*(result[0] == 0?1:result[0]);
                    result[0] = 0;
                }
                else{
                    if( result[0] == 0){
                        result[0] = result[1]*nums[i];
                        result[1] = 0;
                    }else{
                        result[0] = result[0]*result[1]*nums[i];
                        result[1] = 0;
                    }
                }
            }
        right_left = Math.max(Math.max(result[0],target),right_left);
        }

        return Math.max(left_right,right_left);
    }
}
时间: 2024-12-18 23:11:20

leetcode 152. Maximum Product Subarray --------- java的相关文章

[LeetCode] 152. Maximum Product Subarray 求最大子数组乘积

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]

LeetCode 152. Maximum Product Subarray (最大乘积子数组)

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 题目标签:Array, Dynamic Programming 题目给了我们一个nu

Java for LeetCode 152 Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 解题思路: 计算连续的积最大,由于会有负数出现,因此需要用两个int表示包含num

(Java) LeetCode 152. Maximum Product Subarray —— 乘积最大子序列

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]

C#解leetcode 152. Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 分析:这个题目就是让你求连续的子数组的乘积的最大值. (1)在数组中没有0的情况下,

152. Maximum Product Subarray java solutions

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 记录当前最大, 最小值. 因为遇到负数时, 与最小值的乘积可能成为最大值. 1 pu

[动态规划] leetcode 152 Maximum Product Subarray

problem:https://leetcode.com/problems/maximum-product-subarray 类似买卖股票,需要维护两个状态,当前最大数和最小数. class Solution { public: int maxProduct(vector<int>& nums) { int res = INT_MIN; int num_max = 1; int num_min = 1; for(int i = 0;i<nums.size();i++) { int

152. Maximum Product Subarray(js)

152. Maximum Product Subarray 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

刷题152. Maximum Product Subarray

一.题目说明 题目152. Maximum Product Subarray,给一列整数,求最大连续子序列,其乘积最大.难度是Medium! 二.我的解答 这个题目,用双重循环就可以了. class Solution{ public: int maxProduct(vector<int>& nums){ if(nums.size()<=1) return nums[0]; product = INT_MIN; int len = nums.size(); for(int i=0;