首页 > 其他分享 >【LeeCode】152. 乘积最大子数组

【LeeCode】152. 乘积最大子数组

时间:2023-01-04 22:02:38浏览次数:63  
标签:152 乘积 nums int max maxProduct Solution LeeCode new

【题目描述】

给你一个整数数组 ​​nums​​ ,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。

测试用例的答案是一个 32-位 整数。

子数组 是数组的连续子序列。

​https://leetcode.cn/problems/maximum-product-subarray/?favorite=2cktkvj​


【示例】

【LeeCode】152. 乘积最大子数组_Math


【代码】​​画手大鹏​

【LeeCode】152. 乘积最大子数组_最小值_02

package com.company;
// 2023-1-4

class Solution {
public int maxProduct(int[] nums) {
int res = Integer.MIN_VALUE;
int max = 1;
int min = 1;
for (int i = 0; i < nums.length; i++) {
if (nums[i] < 0){
// 如果当前数小于0, 则最大变最小,最小变最大
// 当遇到负数的时候进行交换,因为阶段最小*负数就变阶段最大了,反之同理
int tmp = max;
max = min;
min = tmp;
}
// 对于最小值来说,最小值是本身则说明这个元素值比前面连续子数组的最小值还小。
// 相当于重置了阶段最小值的起始位置
max = Math.max(nums[i], max * nums[i]);
min = Math.min(nums[i], min * nums[i]);
//对比阶段最大值和结果最大值
res = Math.max(max, res);
}
// System.out.println(res);
return res;
}
}

public class Test{
public static void main(String[] args) {
new Solution().maxProduct(new int[]{2,3,-2,4}); // 输出 6
new Solution().maxProduct(new int[]{-2, 0, -1}); // 输出 0
new Solution().maxProduct(new int[]{-4, -3}); // 输出 12
new Solution().maxProduct(new int[]{0, 2}); // 输出 2
new Solution().maxProduct(new int[]{0, -2}); // 输出 0
new Solution().maxProduct(new int[]{3, -1,4}); // 输出 4
new Solution().maxProduct(new int[]{-2, 3, -4}); // 输出 24
new Solution().maxProduct(new int[]{-2, -3, 7}); // 输出 42
new Solution().maxProduct(new int[]{2,-5,-2,-4,3}); // 输出 24 -- 本例代码有误
}
}

【代码】​​钰娘娘​

【LeeCode】152. 乘积最大子数组_子数组_03


class Solution {
public int maxProduct(int[] nums) {
int ans = Integer.MIN_VALUE;
int max = 1;
int min = 1;
for(int num:nums){
int nextmax = Math.max(min*num,Math.max(num,max*num));
int nextmin = Math.min(min*num,Math.min(num,max*num));
max = nextmax;
min = nextmin;
ans = Math.max(max,ans);
}
return ans;
}
}

作者:钰娘娘

标签:152,乘积,nums,int,max,maxProduct,Solution,LeeCode,new
From: https://blog.51cto.com/u_13682316/5989319

相关文章

  • 【LeeCode】198. 打家劫舍
    【题目描述】你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚......
  • leetcode-628. 三个数的最大乘积
    628.三个数的最大乘积-力扣(Leetcode)排序后取最小的两个数和最大的数的乘积与前三大的乘积的最大值,防止最小的两个数是负数,负负得正取这5个数的过程,其实可以直接一次遍......
  • 【LeeCode】2443. 反转之后的数字和
    【题目描述】给一个 非负 整数 ​​num​​ 。如果存在某个 非负 整数 ​​k​​​ 满足 ​​k+reverse(k)=num​​​ ,则返回 ​​true​​ ;否则,返回 ​......
  • 【LeeCode】14. 最长公共前缀
    【题目描述】编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 ​​""​​。​​​​https://leetcode.cn/problems/longest-common-prefix......
  • 【LeeCode】58. 最后一个单词的长度
    【题目描述】给你一个字符串 ​​s​​,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中 最后一个 单词的长度。单词 是指仅由字母组成、不包含任何空格字符的......
  • 【LeeCode】118. 杨辉三角
    【题目描述】给定一个非负整数numRows,生成「杨辉三角」的前numRows行。在「杨辉三角」中,每个数是它左上方和右上方的数的和。​​https://leetcode.cn/problems/pascals-......
  • 【LeeCode】461. 汉明距离
    【题目描述】两个整数之间的 ​​汉明距离​​ 指的是这两个数字对应二进制位不同的位置的数目。给你两个整数 ​​x​​​ 和 ​​y​​,计算并返回它们之间的汉明距离......
  • 【LeeCode】9. 回文数
    【题目描述】回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,​​121​​​ 是回文,而 ​​123​​ 不是。​​​​https://leetcode.cn/problems/palindrom......
  • Leetcode[6279]. 数组乘积中的不同质因数数目
    1.题目给你一个正整数数组 nums ,对 nums 所有元素求积之后,找出并返回乘积中 不同质因数 的数目。注意:质数 是指大于 1 且仅能被 1 及自身整除的数字。如果......
  • 【题解】P1527 [国家集训队]矩阵乘法(整体二分)
    [国家集训队]矩阵乘法题目描述给你一个\(n\timesn\)的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第\(k\)小数。输入格式第一行有两个整数,分别表示矩阵大小\(n......