628:三个数的最大乘积

This commit is contained in:
huangge1199@hotmail.com 2021-04-27 22:56:14 +08:00
parent 176aac0d74
commit e30cc3a8f5
2 changed files with 92 additions and 0 deletions

View File

@ -0,0 +1,58 @@
//给你一个整型数组 nums 在数组中找出由三个数组成的最大乘积并输出这个乘积
//
//
//
// 示例 1
//
//
//输入nums = [1,2,3]
//输出6
//
//
// 示例 2
//
//
//输入nums = [1,2,3,4]
//输出24
//
//
// 示例 3
//
//
//输入nums = [-1,-2,-3]
//输出-6
//
//
//
//
// 提示
//
//
// 3 <= nums.length <= 104
// -1000 <= nums[i] <= 1000
//
// Related Topics 数组 数学
// 👍 295 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
//628:三个数的最大乘积
public class MaximumProductOfThreeNumbers {
public static void main(String[] args) {
//测试代码
Solution solution = new MaximumProductOfThreeNumbers().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int maximumProduct(int[] nums) {
Arrays.sort(nums);
return Math.max(nums[nums.length - 1] * nums[nums.length - 2] * nums[nums.length - 3], nums[nums.length - 1] * nums[0] * nums[1]);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,34 @@
<p>给你一个整型数组 <code>nums</code> ,在数组中找出由三个数组成的最大乘积,并输出这个乘积。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,3]
<strong>输出:</strong>6
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,3,4]
<strong>输出:</strong>24
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>nums = [-1,-2,-3]
<strong>输出:</strong>-6
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>3 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>数学</li></div></div>\n<div><li>👍 295</li><li>👎 0</li></div>