Skip to content

Commit 3fff375

Browse files
committed
feat: add solutions to leetcode problem: No.0628
1 parent 9e135c8 commit 3fff375

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

solution/0600-0699/0628.Maximum Product of Three Numbers/README.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,31 @@
3939
<!-- 这里可写当前语言的特殊实现逻辑 -->
4040

4141
```python
42-
42+
class Solution:
43+
def maximumProduct(self, nums: List[int]) -> int:
44+
n = len(nums)
45+
nums.sort()
46+
# 全负 0 1 n-1
47+
# 全正 n-1 n-2 n-3
48+
# 有正有负 max([0 1 n-1], [n-1 n-2 n-3])
49+
return max(nums[0] * nums[1] * nums[n - 1], nums[n - 1] * nums[n - 2] * nums[n - 3])
4350
```
4451

4552
### **Java**
4653

4754
<!-- 这里可写当前语言的特殊实现逻辑 -->
4855

4956
```java
50-
57+
class Solution {
58+
public int maximumProduct(int[] nums) {
59+
Arrays.sort(nums);
60+
int n = nums.length;
61+
// 全负 0 1 n-1
62+
// 全正 n-1 n-2 n-3
63+
// 有正有负 max([0 1 n-1], [n-1 n-2 n-3])
64+
return Math.max(nums[0] * nums[1] * nums[n - 1], nums[n - 1] * nums[n - 2] * nums[n - 3]);
65+
}
66+
}
5167
```
5268

5369
### **...**

solution/0600-0699/0628.Maximum Product of Three Numbers/README_EN.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,23 @@
4646
### **Python3**
4747

4848
```python
49-
49+
class Solution:
50+
def maximumProduct(self, nums: List[int]) -> int:
51+
n = len(nums)
52+
nums.sort()
53+
return max(nums[0] * nums[1] * nums[n - 1], nums[n - 1] * nums[n - 2] * nums[n - 3])
5054
```
5155

5256
### **Java**
5357

5458
```java
55-
59+
class Solution {
60+
public int maximumProduct(int[] nums) {
61+
Arrays.sort(nums);
62+
int n = nums.length;
63+
return Math.max(nums[0] * nums[1] * nums[n - 1], nums[n - 1] * nums[n - 2] * nums[n - 3]);
64+
}
65+
}
5666
```
5767

5868
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution {
2+
public int maximumProduct(int[] nums) {
3+
Arrays.sort(nums);
4+
int n = nums.length;
5+
return Math.max(nums[0] * nums[1] * nums[n - 1], nums[n - 1] * nums[n - 2] * nums[n - 3]);
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def maximumProduct(self, nums: List[int]) -> int:
3+
n = len(nums)
4+
nums.sort()
5+
return max(nums[0] * nums[1] * nums[n - 1], nums[n - 1] * nums[n - 2] * nums[n - 3])

0 commit comments

Comments
 (0)