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
struct Solution;
use std::cmp::Ordering::*;
impl Solution {
fn third_max(nums: Vec<i32>) -> i32 {
let mut max1 = nums[0];
let mut m2: Option<i32> = None;
let mut m3: Option<i32> = None;
for x in nums {
match x.cmp(&max1) {
Greater => {
m3 = m2;
m2 = Some(max1);
max1 = x;
}
Less => {
if let Some(max2) = m2 {
match x.cmp(&max2) {
Greater => {
m3 = m2;
m2 = Some(x);
}
Less => {
if let Some(max3) = m3 {
if x > max3 {
m3 = Some(x);
}
} else {
m3 = Some(x);
}
}
Equal => {}
}
} else {
m2 = Some(x);
}
}
Equal => {}
}
}
if let Some(max3) = m3 {
max3
} else {
max1
}
}
}
#[test]
fn test() {
let nums = vec![3, 2, 1];
assert_eq!(Solution::third_max(nums), 1);
let nums = vec![1, 2];
assert_eq!(Solution::third_max(nums), 2);
let nums = vec![2, 2, 3, 1];
assert_eq!(Solution::third_max(nums), 1);
let nums = vec![1, 2, 2, 5, 3, 5];
assert_eq!(Solution::third_max(nums), 2);
}