We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6ca51f3 commit 8a3226eCopy full SHA for 8a3226e
Algorithms/Easy/414_ThirdMaximumNumber/Solution.java
@@ -0,0 +1,27 @@
1
+class Solution {
2
+ public int thirdMax(int[] nums) {
3
+ long max1 = -2147483649l;
4
+ long max2 = -2147483649l;
5
+ long max3 = -2147483649l;
6
+
7
8
+ for (int i = 0; i < nums.length; i++) {
9
+ if (nums[i] > max1) {
10
+ max3 = max2;
11
+ max2 = max1;
12
+ max1 = nums[i];
13
+ } else if (nums[i] > max2 && nums[i] < max1) {
14
15
+ max2 = nums[i];
16
+ } else if (nums[i] > max3 && nums[i] < max2) {
17
+ max3 = nums[i];
18
+ }
19
20
21
+ if (max3 == -2147483649l) {
22
+ return (int)max1;
23
24
25
+ return (int)max3;
26
27
+}
0 commit comments