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的元素,首先分析当数组中有<0的元素,可分为2种情况:

  1)个数为偶数,这种情况只要把元素全乘一遍就会得到最大值;

  2)个数为奇数,首先看一个例子:

[-1, -2, -3, -4, -5]

//因为题目所要求返回的最大乘积,所以可以排除无用的排序将其分为两个数组
[-1, -2, -3, -4]

[-2, -3, -4, -5]

    因此,奇数的情况只有两种可能性,分别是 从第一个元素乘到倒数第二个负数 和 从第二个负数乘到最后一个元素。

   而>0的元素没有丝毫的影响,乘过去即可。就算负数与负数的中间或是前面或是后面夹杂着正数也只是扩大最后的结果。

2) 有0的元素,无论哪个数乘0都为0,所以0就相当一个终止符,把一个数组划分成两个数组。

代码:

 1 class Solution {
 2 public:
 3     int maxProduct(vector<int>& nums) {
 4         int maxAll = nums[0], n = nums.size();
 5         int first = 1, second = 1;
 6         bool flag = 0; //是否遇到负数标志
 7         for(int i = 0; i < n; i++){
 8             first *= nums[i];
 9             second *= nums[i];
10             maxAll = maxAll>first?maxAll:first;  //负数是奇数的第一个情况
11             maxAll = maxAll>second?maxAll:second;  //奇数的第二个情况
12             if(nums[i] < 0 && !flag){
13             //第一次遇到负数
14                 second = 1;
15                 flag = 1;
16             }
17             if(nums[i] == 0){
18             //遇到0的情况
19                 maxAll = 0>maxAll?0:maxAll;
20                 flag = 0;-5
21                 first = 1;
22                 second = 1;
23             }
24         }
25         return maxAll;
26     }
27 };

原文地址:https://www.cnblogs.com/sakuya0000/p/8783340.html

时间: 2024-10-07 05:43:48

152. Maximum Product Subarray 解题记录的相关文章

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;

【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. 题解: 先暴力解,遍历所有组合,更新最大值.很显然得超时. Solution

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

[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]

152. Maximum Product Subarray (Array; DP)

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. 思路:类似Maximum Subarray,不同的是 max(max_local*n

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 --------- 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. 找出最大的相乘的数字,很简单,代码还可以优化的地方很多.但是速度还可以. publi