当前位置:网站首页>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;
}
}
边栏推荐
- C#实战:基于ItextSharp技术标签生成小工具
- what is bsp in rtems
- MongoDB database notes
- 越折腾越好用的 3 款开源 APP
- 4 面拿华为 offer 的水平,面试阿里居然一面就被吊打?
- 谷歌数据中心发生“电力事故”造成 3 人受伤
- 第二十二章 源代码文件 REST API 参考(四)
- 【Azure云】服务端点和私有链接有什么区别?观点(1)
- Regarding the missing json converter, the error message is: No converter found for return value of type
- In August the DB list latest scores - database Engines
猜你喜欢

Dry goods!ASSANet: Making PointNet++ faster and stronger

这些年我开源的几个小项目

14 high-frequency handwritten JS interview questions and answers to consolidate your JS foundation

String interception function in SQL

Redis设计与实现

Mobile and PC compatible loading and toast message plugins

3D旋转文本动画js特效

网络安全笔记6——数字证书与公钥基础设施

「业务架构」介绍BPMN第二部分-泳道

组合模式:Swift 实现
随机推荐
Taro小程序跨端开发入门实战
AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)
[C language] Header file #include
, conio is Console Input/Output (console input and output) Pycharm终端出现PS问题、conda或activate不是内部命令问题..
金九银十跳槽旺季:阿里、百度、京东、美团等技术面试题及答案
"Data Strategy" Results-Driven Enterprise Data Strategy: Organization and Governance
开发模式对测试的影响
是什么影响了MySQL性能?
关于“码农”的一点自嘲解构
mysql appears: ERROR 1524 (HY000): Plugin '123' is not loaded
自动化测试及Selenium
【勇敢饭饭,不怕刷题之链表】链表反转的几种情况
Memory problems difficult to locate, it is because you do not use ASAN
Break through the dimensional barriers and let the dolls around you move on the screen!
"Chief Engineer" Principal (Principal) engineer's way of training
Network Security Note 6 - Digital Certificates and Public Key Infrastructure
从产品角度看 L2 应用:为什么说这是一个游乐场?
2022.8.8-----leetcode.761
Gartner reiterates the important value of 'data weaving'
第2章-矩阵及其运算-矩阵运算(2)