当前位置:网站首页>LeetCode_628_三个数的最大乘积
LeetCode_628_三个数的最大乘积
2022-08-10 10:43:00 【Fitz1318】
题目链接
题目描述
给你一个整型数组 nums ,在数组中找出由三个数组成的最大乘积,并输出这个乘积。
示例 1:
输入:nums = [1,2,3]
输出:6
示例 2:
输入:nums = [1,2,3,4]
输出:24
示例 3:
输入:nums = [-1,-2,-3]
输出:-6
提示:
3 <= nums.length <= 10^4-1000 <= nums[i] <= 1000
解题思路
对数组的构成进行分情况讨论
- 如果全是正数:
- 选择最大的三个正数,即最大的三个数
- 如果至少两个正数,只有一个负数:
- 至少有三个正数:选择最大的三个正数,即最大的三个数
- 只有两个正数:只有三个数可以选择,即最大的三个数
- 如果至少一个正数,只有两个负数:
- 至少有三个正数:选择最大的三个正数,即最大的三个数,或者是选择两个负数和最大的一个正数,即最小的两个数和最大的一个数
- 只有两个正数:选择两个负数和最大的一个正数,即最小的两个数和最大的一个数
- 只有一个正数:只有三个数可以选择,即最大的三个数
- 如果全是负数:
- 选择最大的三个负数,即最大的三个数
综上所述,结果为最大的三个数和最小的两个数和最大的一个数中的更大值
AC代码
class Solution {
public int maximumProduct(int[] nums) {
Arrays.sort(nums);
int len = nums.length;
int ans = 0;
ans = Math.max(nums[len - 1] * nums[len - 2] * nums[len - 3], nums[len - 1] * nums[0] * nums[1]);
return ans;
}
}
边栏推荐
- 【FAQ】【Push Kit】 华为怎么设置角标
- Weilai-software development engineer side record
- Automated Testing and Selenium
- Gartner reiterates the important value of 'data weaving'
- Mobile and PC compatible loading and toast message plugins
- 【电商运营】你真的了解社交媒体营销(SMM)吗?
- PPT | 「课件」企业中高层人员安全管理培训(118页)
- STM32 encapsulation ESP8266 a key configuration function: implementations of AP mode and the STA mode switch, server and the client to create
- 网络安全笔记6——数字证书与公钥基础设施
- js guessing game source code
猜你喜欢

蔚来-软件开发工程师一面记录

自动化测试及Selenium

4 of huawei offer levels, incredibly side is easing the bit in the interview ali?

OneFlow source code parsing: operator instructions executed in a virtual machine

开发模式对测试的影响

什么是抽象类

runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function

让软件飞——“X+”技术揭秘

leetcode:334. 递增的三元子序列

Redis(三)——配置文件详解、发布和订阅、新数据类型
随机推荐
解决ASP.NET Core在Task中使用IServiceProvider的问题
Text selection rounded style border-radius
【勇敢饭饭,不怕刷题之链表】链表反转的几种情况
兼容移动和PC的loading加载和toast消息插件
Three-phase 380V rectified voltage
4 of huawei offer levels, incredibly side is easing the bit in the interview ali?
第二十二章 源代码文件 REST API 参考(四)
ISO9001在讲什么?过程方法和风险思维
关于json转换器缺失的问题,报错内容:No converter found for return value of type
WebView2 通过 PuppeteerSharp 实现爬取 王者 壁纸 (案例版)
金九银十跳槽旺季:阿里、百度、京东、美团等技术面试题及答案
Pycharm终端出现PS问题、conda或activate不是内部命令问题..
database transaction
C#实战:基于ItextSharp技术标签生成小工具
Redis6 (1) - Introduction to NoSQL Database and Installation of Redis
chart.js horizontal column chart plugin
「首席工程师」首席(Principal )工程师修炼之道
PPT | 「课件」企业中高层人员安全管理培训(118页)
What is affecting MySQL performance?
Double.doubleToLongBits()方法使用