Leetcode Note: Go - Third Maximum Number
Third Maximum Number - LeetCode
https://leetcode.com/problems/third-maximum-number/
- Go 言語で取り組んだメモ
所感
- 数値配列から3番めに数値が大きい要素を int で return する
回答
O(1) Space [Golang, Go] - LeetCode Discuss
https://leetcode.com/problems/third-maximum-number/discuss/753661/O(1)-Space-Golang-Go
func thirdMax(nums []int) int {
max, second, third := math.MinInt64, math.MinInt64, math.MinInt64
for _, v := range nums {
if v == max || v == second || v == third {
continue
}
switch {
case v > max:
max, second, third = v, max, second
case v > second:
second, third = v, second
case v > third:
third = v
}
}
if third == math.MinInt64 {
return max
}
return third
}