LeetCode 628. 三个数的最大乘积

题目描述

  • LeetCode 628. 三个数的最大乘积
  • 给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积。

示例1

  • 输入: [1,2,3]
  • 输出: 6

示例2

  • 输入: [1,2,3,4]
  • 输出: 24

Java Code

class Solution {
    public  int maximumProduct(int[] nums) {
        int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE;
        int min2 = Integer.MAX_VALUE, min1 = Integer.MAX_VALUE;
        for (int num : nums) {
            if (num >= max1) {
                max3 = max2;
                max2 = max1;
                max1 = num;
            } else if (num >= max2) {
                max3 = max2;
                max2 = num;
            } else if (num >= max3) {
                max3 = num;
            }

            if (num <= min1) {
                min2 = min1;
                min1 = num;
            } else if (num <= min2) {
                min2 = num;
            }
        }
        return Math.max(max1 * max2 * max3, max1 * min1 * min2);
    }
}

参考链接:

原文地址:https://www.cnblogs.com/hglibin/p/10262634.html

时间: 2024-07-30 13:23:24

LeetCode 628. 三个数的最大乘积的相关文章

[LeetCode] 628. 三个数的最大乘积 ☆

描述 给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积. 示例 1: 输入: [1,2,3]输出: 6示例 2: 输入: [1,2,3,4]输出: 24注意: 给定的整型数组长度范围是[3,104],数组中所有的元素范围是[-1000, 1000].输入的数组中任意三个数的乘积不会超出32位有符号整数的范围. 解析 3个最大值的可能情况:3个正数   2个负数+1个正数. 代码 public static int maximumProduct(int[] nums) { if

LeetCode:三个数的最大乘积【628】

LeetCode:三个数的最大乘积[628] 题目描述 给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积. 示例 1: 输入: [1,2,3] 输出: 6 示例 2: 输入: [1,2,3,4] 输出: 24 注意: 给定的整型数组长度范围是[3,104],数组中所有的元素范围是[-1000, 1000]. 输入的数组中任意三个数的乘积不会超出32位有符号整数的范围. 题目分析 仔细审题,我们发现,数组中元素的可以是负数,这也就说明,不一定是排序后的后三位数字是最大乘积. 那

力扣(LeetCode)三个数的最大乘积 个人题解

给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积. 示例 1: 输入: [1,2,3] 输出: 6 示例 2: 输入: [1,2,3,4] 输出: 24 注意: 给定的整型数组长度范围是[3,104],数组中所有的元素范围是[-1000, 1000]. 输入的数组中任意三个数的乘积不会超出32位有符号整数的范围. 因为这题里出现了负数,所以最大值并不是简单得取三个最大值就好了. 参考了评论区的做法,将这个问题分解为:三个最大值的乘积 和 两个最小值和一个最大值的乘积 二者之间

LeetCode 628. Maximum Product of Three Numbers三个数的最大乘积 (C++)

题目: Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4] Output: 24 分析: 给定一个数组,返回其中三个元素乘积的最大值. 注意的是,这道题是可以有负数出现,且是求三个数的乘积,所以我们需要考虑负数的情况. 最先

【LeetCode】数组-2(628)-数组中三个数相乘最大

题目不难: 思路一(排序取两端) 先排序,最后三个数相乘即可.(很快就想到了,但是没想全面 [??] ) 缺陷:没有考虑到有负数的情况,当至少有两个负数时,需要判断 最大数乘两个最小的负数 和 三个最大数相乘的大小,返回大的. 代码如下: public class Solution { public int maximumProduct(int[] nums) { Arrays.sort(nums); return Math.max(nums[nums.length - 1] * nums[nu

LeetCode 16 3Sum Closest 找出最接近指定target的三个数的和

题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-

数组任意取三个数中乘积最大值

一.给定一个整型数组,包括正负值,找出取任意三个值的乘积最大 1.对整型排序(这里使用堆排序) //堆排序 private static void headSort(int arr[], int len) { int s = len / 2; for (int i = s; i >= 0; i--) { hSort(arr, i, len); } for (int i = len; i >0 ; i--) { swap(arr, 0 , i); hSort(arr, 0, i-1); } }

Leetcode - 628 Maximum Product of Three Numbers

Leetcode - 628 Maximum Product of Three Numbers 628. Maximum Product of Three Numbers Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4]

动态规划题目(三)——最大连续乘积子串

动态规划题目(三)--最大连续乘积子串 1. 题目描述 给一个浮点数序列,取最大乘积连续子串的值,例如 -2.5,4,0,3,0.5,8,-1,则取出的最大乘积连续子串为3,0.5,8.也就是说,上述数组中,3 0.5 8这3个数的乘积30.58=12是最大的,而且是连续的. 2. 动态规划求解 动态规划求解题目的时候最重要的是要找到状态转移方程! 针对这道题目,我们使用两个变量记录当前最大值maxEnd, 和当前最小值minEnd.为什么记录当前最小值呢?因为数组中会出现负数,乘以一个负数的话