-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path628-MaximumProductOfThreeNumbers.go
More file actions
61 lines (51 loc) · 1.43 KB
/
628-MaximumProductOfThreeNumbers.go
File metadata and controls
61 lines (51 loc) · 1.43 KB
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
package main
// 628. Maximum Product of Three Numbers
// Given an integer array nums, find three numbers whose product is maximum and return the maximum product.
// Example 1:
// Input: nums = [1,2,3]
// Output: 6
// Example 2:
// Input: nums = [1,2,3,4]
// Output: 24
// Example 3:
// Input: nums = [-1,-2,-3]
// Output: -6
// Constraints:
// 3 <= nums.length <= 10^4
// -1000 <= nums[i] <= 1000
import "fmt"
import "sort"
// func maximumProduct(nums []int) int {
// sort.Ints(nums)
// res, arr := 1, nums[len(nums) - 3:]
// for _, v := range arr {
// res *= v
// }
// return res
// }
func maximumProduct(nums []int) int {
sort.Ints(nums)
sum1 := nums[len(nums)-3] * nums[len(nums)-2] * nums[len(nums)-1] // 后三位的乘积
if nums[0] < 0 && nums[1] < 0 && nums[len(nums)-1] > 0 { // 如果前面两位都为负数
sum2 := nums[0] * nums[1] * nums[len(nums)-1]
if sum2 > sum1 {
return sum2
}
}
return sum1
}
func main() {
// Example 1:
// Input: nums = [1,2,3]
// Output: 6
fmt.Println(maximumProduct([]int{1,2,3})) // 6
// Example 2:
// Input: nums = [1,2,3,4]
// Output: 24
fmt.Println(maximumProduct([]int{1,2,3,4})) // 24
// Example 3:
// Input: nums = [-1,-2,-3]
// Output: -6
fmt.Println(maximumProduct([]int{-1,-2,-3})) // -6
fmt.Println(maximumProduct([]int{-100,-98,-1,2,3,4})) // 39200 4 * -98 * -100
}