mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-11 06:05:20 +08:00
添加 problem 414
This commit is contained in:
@ -0,0 +1,25 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
func thirdMax(nums []int) int {
|
||||||
|
a, b, c := math.MinInt64, math.MinInt64, math.MinInt64
|
||||||
|
for _, v := range nums {
|
||||||
|
if v > a {
|
||||||
|
c = b
|
||||||
|
b = a
|
||||||
|
a = v
|
||||||
|
} else if v < a && v > b {
|
||||||
|
c = b
|
||||||
|
b = v
|
||||||
|
} else if v < b && v > c {
|
||||||
|
c = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if c == math.MinInt64 {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type question414 struct {
|
||||||
|
para414
|
||||||
|
ans414
|
||||||
|
}
|
||||||
|
|
||||||
|
// para 是参数
|
||||||
|
// one 代表第一个参数
|
||||||
|
type para414 struct {
|
||||||
|
one []int
|
||||||
|
}
|
||||||
|
|
||||||
|
// ans 是答案
|
||||||
|
// one 代表第一个答案
|
||||||
|
type ans414 struct {
|
||||||
|
one int
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Problem414(t *testing.T) {
|
||||||
|
|
||||||
|
qs := []question414{
|
||||||
|
|
||||||
|
question414{
|
||||||
|
para414{[]int{1, 1, 2}},
|
||||||
|
ans414{2},
|
||||||
|
},
|
||||||
|
|
||||||
|
question414{
|
||||||
|
para414{[]int{3, 2, 1}},
|
||||||
|
ans414{1},
|
||||||
|
},
|
||||||
|
|
||||||
|
question414{
|
||||||
|
para414{[]int{1, 2}},
|
||||||
|
ans414{2},
|
||||||
|
},
|
||||||
|
|
||||||
|
question414{
|
||||||
|
para414{[]int{2, 2, 3, 1}},
|
||||||
|
ans414{1},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("------------------------Leetcode Problem 414------------------------\n")
|
||||||
|
|
||||||
|
for _, q := range qs {
|
||||||
|
_, p := q.ans414, q.para414
|
||||||
|
fmt.Printf("【input】:%v 【output】:%v\n", p, thirdMax(p.one))
|
||||||
|
}
|
||||||
|
fmt.Printf("\n\n\n")
|
||||||
|
}
|
40
Algorithms/0414. Third Maximum Number/README.md
Normal file
40
Algorithms/0414. Third Maximum Number/README.md
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
# [414. Third Maximum Number](https://leetcode.com/problems/third-maximum-number/)
|
||||||
|
|
||||||
|
## 题目
|
||||||
|
|
||||||
|
Given a **non-empty** array of integers, return the **third** maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).
|
||||||
|
|
||||||
|
**Example 1:**
|
||||||
|
|
||||||
|
Input: [3, 2, 1]
|
||||||
|
|
||||||
|
Output: 1
|
||||||
|
|
||||||
|
Explanation: The third maximum is 1.
|
||||||
|
|
||||||
|
**Example 2:**
|
||||||
|
|
||||||
|
Input: [1, 2]
|
||||||
|
|
||||||
|
Output: 2
|
||||||
|
|
||||||
|
Explanation: The third maximum does not exist, so the maximum (2) is returned instead.
|
||||||
|
|
||||||
|
**Example 3:**
|
||||||
|
|
||||||
|
Input: [2, 2, 3, 1]
|
||||||
|
|
||||||
|
Output: 1
|
||||||
|
|
||||||
|
Explanation: Note that the third maximum here means the third maximum distinct number.
|
||||||
|
Both numbers with value 2 are both considered as second maximum.
|
||||||
|
|
||||||
|
|
||||||
|
## 题目大意
|
||||||
|
|
||||||
|
给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是 O(n)。
|
||||||
|
|
||||||
|
|
||||||
|
## 解题思路
|
||||||
|
|
||||||
|
- 水题,动态维护 3 个最大值即可。注意数组中有重复数据的情况。如果只有 2 个数或者 1 个数,则返回 2 个数中的最大值即可。
|
Reference in New Issue
Block a user