FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode/src/main/java/L0414_ThirdMaximumNumber.java at master · LjyYano/LeetCode · GitHub
LjyYano
/
LeetCode
Public
Notifications
You must be signed in to change notification settings
Fork
125
Star
342
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode
/
src
/
main
/
java
/
L0414_ThirdMaximumNumber.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
83 lines (73 loc) · 2.9 KB
Breadcrumbs
LeetCode
/
src
/
main
/
java
/
L0414_ThirdMaximumNumber.java
Copy path
File metadata and controls
83 lines (73 loc) · 2.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import
java
.
util
.*;
/**
* https://leetcode.cn/problems/third-maximum-number/description/
*
* 给你一个非空数组,返回此数组中 第三大的数 。如果不存在,则返回数组中最大的数。
*
* 示例 1:
* 输入:[3, 2, 1]
* 输出:1
* 解释:第三大的数是 1 。
*
* 示例 2:
* 输入:[1, 2]
* 输出:2
* 解释:第三大的数不存在, 所以返回最大的数 2 。
*
* 示例 3:
* 输入:[2, 2, 3, 1]
* 输出:1
* 解释:注意,要求返回第三大的数,是指在所有不同数字中排第三大的数。
* 此例中存在两个值为 2 的数,它们都排第二。在所有不同数字中排第三大的数为 1 。
*/
public
class
L0414_ThirdMaximumNumber
{
public
int
thirdMax
(
int
[]
nums
) {
// 使用 Long 类型是为了处理 Integer.MIN_VALUE 的情况
Long
firstMax
=
null
;
Long
secondMax
=
null
;
Long
thirdMax
=
null
;
for
(
int
num
:
nums
) {
Long
n
=
Long
.
valueOf
(
num
);
// 跳过重复的数字
if
(
n
.
equals
(
firstMax
) ||
n
.
equals
(
secondMax
) ||
n
.
equals
(
thirdMax
)) {
continue
;
}
// 更新三个最大值
if
(
firstMax
==
null
||
n
>
firstMax
) {
thirdMax
=
secondMax
;
secondMax
=
firstMax
;
firstMax
=
n
;
}
else
if
(
secondMax
==
null
||
n
>
secondMax
) {
thirdMax
=
secondMax
;
secondMax
=
n
;
}
else
if
(
thirdMax
==
null
||
n
>
thirdMax
) {
thirdMax
=
n
;
}
}
// 如果第三大的数不存在,返回最大的数
return
thirdMax
==
null
?
firstMax
.
intValue
() :
thirdMax
.
intValue
();
}
public
static
void
main
(
String
[]
args
) {
L0414_ThirdMaximumNumber
solution
=
new
L0414_ThirdMaximumNumber
();
// 测试用例1
int
[]
nums1
= {
3
,
2
,
1
};
System
.
out
.
println
(
"测试用例1:"
);
System
.
out
.
println
(
"输入:"
+
Arrays
.
toString
(
nums1
));
System
.
out
.
println
(
"输出:"
+
solution
.
thirdMax
(
nums1
));
// 测试用例2
int
[]
nums2
= {
1
,
2
};
System
.
out
.
println
(
"
\n
测试用例2:"
);
System
.
out
.
println
(
"输入:"
+
Arrays
.
toString
(
nums2
));
System
.
out
.
println
(
"输出:"
+
solution
.
thirdMax
(
nums2
));
// 测试用例3
int
[]
nums3
= {
2
,
2
,
3
,
1
};
System
.
out
.
println
(
"
\n
测试用例3:"
);
System
.
out
.
println
(
"输入:"
+
Arrays
.
toString
(
nums3
));
System
.
out
.
println
(
"输出:"
+
solution
.
thirdMax
(
nums3
));
// 测试用例4:包含 Integer.MIN_VALUE
int
[]
nums4
= {
1
,
Integer
.
MIN_VALUE
,
2
};
System
.
out
.
println
(
"
\n
测试用例4:"
);
System
.
out
.
println
(
"输入:"
+
Arrays
.
toString
(
nums4
));
System
.
out
.
println
(
"输出:"
+
solution
.
thirdMax
(
nums4
));
}
}
Back
|
FazBrowse Home
|
New Git URL