414. Third Maximum Number Easy
1/**
2 * [414] Third Maximum Number
3 *
4 * Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number.
5 *
6 * Example 1:
7 *
8 * Input: nums = [3,2,1]
9 * Output: 1
10 * Explanation:
11 * The first distinct maximum is 3.
12 * The second distinct maximum is 2.
13 * The third distinct maximum is 1.
14 *
15 * Example 2:
16 *
17 * Input: nums = [1,2]
18 * Output: 2
19 * Explanation:
20 * The first distinct maximum is 2.
21 * The second distinct maximum is 1.
22 * The third distinct maximum does not exist, so the maximum (2) is returned instead.
23 *
24 * Example 3:
25 *
26 * Input: nums = [2,2,3,1]
27 * Output: 1
28 * Explanation:
29 * The first distinct maximum is 3.
30 * The second distinct maximum is 2 (both 2's are counted together since they have the same value).
31 * The third distinct maximum is 1.
32 *
33 *
34 * Constraints:
35 *
36 * 1 <= nums.length <= 10^4
37 * -2^31 <= nums[i] <= 2^31 - 1
38 *
39 *
40 * Follow up: Can you find an O(n) solution?
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/third-maximum-number/
45// discuss: https://leetcode.com/problems/third-maximum-number/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50 pub fn third_max(nums: Vec<i32>) -> i32 {
51 0
52 }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn test_414() {
63 }
64}
65
Back
© 2025 bowen.ge All Rights Reserved.