Skip to content

Commit 822f86a

Browse files
authored
feat: add solutions to lc problem: No.0628 (doocs#881)
No.0628.Maximum Product of Three Numbers
1 parent 350f7d2 commit 822f86a

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

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

+20
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,26 @@ class Solution {
8080
}
8181
```
8282

83+
### **Go**
84+
85+
```go
86+
func maximumProduct(nums []int) int {
87+
n := len(nums)
88+
sort.Ints(nums)
89+
// 全负 0 1 n-1
90+
// 全正 n-1 n-2 n-3
91+
// 有正有负 max([0 1 n-1], [n-1 n-2 n-3])
92+
return max(nums[0]*nums[1]*nums[n-1], nums[n-1]*nums[n-2]*nums[n-3])
93+
}
94+
95+
func max(a, b int) int {
96+
if a > b {
97+
return a
98+
}
99+
return b
100+
}
101+
```
102+
83103
### **...**
84104

85105
```

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

+17
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ class Solution {
5353
}
5454
```
5555

56+
### **Go**
57+
58+
```go
59+
func maximumProduct(nums []int) int {
60+
n := len(nums)
61+
sort.Ints(nums)
62+
return max(nums[0]*nums[1]*nums[n-1], nums[n-1]*nums[n-2]*nums[n-3])
63+
}
64+
65+
func max(a, b int) int {
66+
if a > b {
67+
return a
68+
}
69+
return b
70+
}
71+
```
72+
5673
### **...**
5774

5875
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func maximumProduct(nums []int) int {
2+
n := len(nums)
3+
sort.Ints(nums)
4+
return max(nums[0]*nums[1]*nums[n-1], nums[n-1]*nums[n-2]*nums[n-3])
5+
}
6+
7+
func max(a, b int) int {
8+
if a > b {
9+
return a
10+
}
11+
return b
12+
}

0 commit comments

Comments
 (0)