-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
42 lines (38 loc) · 832 Bytes
/
main.go
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
// https://leetcode-cn.com/problems/third-maximum-number/submissions/
package main
import (
"fmt"
"math"
)
func thirdMax(nums []int) int {
var (
num = int(math.Pow(-2, 31))
max = num
mid = num
min = num
set = make(map[int]struct{})
)
for _, v := range nums {
if v > max {
max, mid, min = v, max, mid
} else if v < max && v > mid {
mid, min = v, mid
} else if v < mid && v > min {
min = v
}
set[v] = struct{}{}
}
if len(set) >= 3 {
return min
} else {
return max
}
}
func main() {
fmt.Println(thirdMax([]int{3, 2, 1}), 1)
fmt.Println(thirdMax([]int{1, 2}), 2)
fmt.Println(thirdMax([]int{2, 2, 3, 1}), 1)
fmt.Println(thirdMax([]int{1, 2, -2147483648}), -2147483648)
fmt.Println(thirdMax([]int{1, -2147483648, 2}), -2147483648)
fmt.Println(thirdMax([]int{5, 2, 4, 1, 3, 6, 0}), 4)
}