-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path628.三个数的最大乘积.js
67 lines (65 loc) · 1.02 KB
/
628.三个数的最大乘积.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* @lc app=leetcode.cn id=628 lang=javascript
*
* [628] 三个数的最大乘积
*
* https://leetcode.cn/problems/maximum-product-of-three-numbers/description/
*
* algorithms
* Easy (52.35%)
* Likes: 379
* Dislikes: 0
* Total Accepted: 95.4K
* Total Submissions: 182.3K
* Testcase Example: '[1,2,3]'
*
* 给你一个整型数组 nums ,在数组中找出由三个数组成的最大乘积,并输出这个乘积。
*
*
*
* 示例 1:
*
*
* 输入:nums = [1,2,3]
* 输出:6
*
*
* 示例 2:
*
*
* 输入:nums = [1,2,3,4]
* 输出:24
*
*
* 示例 3:
*
*
* 输入:nums = [-1,-2,-3]
* 输出:-6
*
*
*
*
* 提示:
*
*
* 3
* -1000
*
*
*/
// @lc code=start
/**
* @param {number[]} nums
* @return {number}
*/
var maximumProduct = function (nums) {
const _nums = nums.sort((a, b) => a - b);
return Math.max(
// 有正有负
_nums[0] * _nums[1] * _nums.at(-1),
// 全都为负数,或者全是正数
_nums.at(-1) * _nums.at(-2) * _nums.at(-3)
);
};
// @lc code=end