Skip to content

Commit 03e1ed0

Browse files
committed
solve problem Third Maximum Number
1 parent 62954de commit 03e1ed0

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ All solutions will be accepted!
151151
|849|[Maximize Distance To Closest Person](https://leetcode-cn.com/problems/maximize-distance-to-closest-person/description/)|[java/py/js](./algorithms/MaximizeDistanceToClosestPerson)|Easy|
152152
|674|[Longest Continuous Increasing Subsequence](https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence/description/)|[java/py/js](./algorithms/LongestContinuousIncreasingSubsequence)|Easy|
153153
|234|[Palindrome Linked List](https://leetcode-cn.com/problems/palindrome-linked-list/description/)|[java/py/js](./algorithms/PalindromeLinkedList)|Easy|
154+
|414|[Third Maximum Number](https://leetcode-cn.com/problems/third-maximum-number/description/)|[java/py/js](./algorithms/ThirdMaximumNumber)|Easy|
154155

155156
# Database
156157
|#|Title|Solution|Difficulty|
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Third Maximum Number
2+
This problem is easy to solve by three times repeat
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public int thirdMax(int[] nums) {
3+
Integer maxNum = null,
4+
secondNum = null,
5+
thirdNum = null;
6+
7+
for (int num : nums) {
8+
if (maxNum == null) {
9+
maxNum = num;
10+
} else {
11+
maxNum = Math.max(maxNum, num);
12+
}
13+
}
14+
15+
for (int num : nums) {
16+
if (num == maxNum) {
17+
continue;
18+
} else if (secondNum == null) {
19+
secondNum = num;
20+
} else {
21+
secondNum = Math.max(secondNum, num);
22+
}
23+
}
24+
25+
for (int num : nums) {
26+
if (num == maxNum || num == secondNum) {
27+
continue;
28+
} else if (thirdNum == null) {
29+
thirdNum = num;
30+
} else {
31+
thirdNum = Math.max(thirdNum, num);
32+
}
33+
}
34+
35+
return thirdNum != null ? thirdNum : maxNum;
36+
}
37+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var thirdMax = function(nums) {
6+
let maxNum = null,
7+
secondNum = null,
8+
thirdNum = null
9+
10+
nums.forEach(num => {
11+
if (maxNum === null) {
12+
maxNum = num
13+
} else {
14+
maxNum = Math.max(maxNum, num)
15+
}
16+
})
17+
18+
nums.forEach(num => {
19+
if (num === maxNum) {
20+
} else if (secondNum === null) {
21+
secondNum = num
22+
} else {
23+
secondNum = Math.max(secondNum, num)
24+
}
25+
})
26+
27+
nums.forEach(num => {
28+
if (num === maxNum || num === secondNum) {
29+
} else if (thirdNum === null) {
30+
thirdNum = num
31+
} else {
32+
thirdNum = Math.max(thirdNum, num)
33+
}
34+
})
35+
36+
37+
return thirdNum !== null ? thirdNum : maxNum
38+
};
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution(object):
2+
def thirdMax(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
max_num = None
8+
second_num = None
9+
third_num = None
10+
11+
for num in nums:
12+
if max_num == None:
13+
max_num = num
14+
else:
15+
max_num = max(max_num, num)
16+
17+
for num in nums:
18+
if num == max_num:
19+
continue
20+
elif second_num == None:
21+
second_num = num
22+
else:
23+
second_num = max(second_num, num)
24+
25+
for num in nums:
26+
if num == max_num or num == second_num:
27+
continue
28+
elif third_num == None:
29+
third_num = num
30+
else:
31+
third_num = max(third_num, num)
32+
33+
return third_num if third_num != None else max_num

0 commit comments

Comments
 (0)