Skip to content

Commit 8a3226e

Browse files
authored
Create Solution.java (Tahanima#94)
1 parent 6ca51f3 commit 8a3226e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -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+
max3 = max2;
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

Comments
 (0)