File tree 3 files changed +49
-0
lines changed
solution/0600-0699/0628.Maximum Product of Three Numbers
3 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -80,6 +80,26 @@ class Solution {
80
80
}
81
81
```
82
82
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
+
83
103
### ** ...**
84
104
85
105
```
Original file line number Diff line number Diff line change @@ -53,6 +53,23 @@ class Solution {
53
53
}
54
54
```
55
55
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
+
56
73
### ** ...**
57
74
58
75
```
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments