classSolution: defthirdMax(self, nums: List[int]) -> int: s = SortedList() for num in nums: if num notin s: s.add(num) iflen(s) > 3: s.pop(0) return s[0] iflen(s) == 3else s[-1]
classSolution: defthirdMax(self, nums: List[int]) -> int: a, b, c = float('-inf'), float('-inf'), float('-inf') for num in nums: if num > a: a, b, c = num, a, b elif a > num > b: b, c = num, b elif b > num > c: c = num return a if c == float('-inf') else c
[sol3-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { public: intthirdMax(vector<int> &nums){ long a = LONG_MIN, b = LONG_MIN, c = LONG_MIN; for (long num : nums) { if (num > a) { c = b; b = a; a = num; } elseif (a > num && num > b) { c = b; b = num; } elseif (b > num && num > c) { c = num; } } return c == LONG_MIN ? a : c; } };
[sol3-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { publicintthirdMax(int[] nums) { longa= Long.MIN_VALUE, b = Long.MIN_VALUE, c = Long.MIN_VALUE; for (long num : nums) { if (num > a) { c = b; b = a; a = num; } elseif (a > num && num > b) { c = b; b = num; } elseif (b > num && num > c) { c = num; } } return c == Long.MIN_VALUE ? (int) a : (int) c; } }
[sol3-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicclassSolution { publicintThirdMax(int[] nums) { long a = long.MinValue, b = long.MinValue, c = long.MinValue; foreach (long num in nums) { if (num > a) { c = b; b = a; a = num; } elseif (a > num && num > b) { c = b; b = num; } elseif (b > num && num > c) { c = num; } } return c == long.MinValue ? (int) a : (int) c; } }
[sol3-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
functhirdMax(nums []int)int { a, b, c := math.MinInt64, math.MinInt64, math.MinInt64 for _, num := range nums { if num > a { a, b, c = num, a, b } elseif a > num && num > b { b, c = num, b } elseif b > num && num > c { c = num } } if c == math.MinInt64 { return a } return c }
[sol3-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var thirdMax = function(nums) { let a = -Number.MAX_VALUE, b = -Number.MAX_VALUE, c = -Number.MAX_VALUE; for (const num of nums) { if (num > a) { c = b; b = a; a = num; } elseif (a > num && num > b) { c = b; b = num; } elseif (b > num && num > c) { c = num; } } return c === -Number.MAX_VALUE ? a : c; };
classSolution: defthirdMax(self, nums: List[int]) -> int: a, b, c = None, None, None for num in nums: if a isNoneor num > a: a, b, c = num, a, b elif a > num and (b isNoneor num > b): b, c = num, b elif b isnotNoneand b > num and (c isNoneor num > c): c = num return a if c isNoneelse c
[sol4-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { public: intthirdMax(vector<int> &nums){ int *a = nullptr, *b = nullptr, *c = nullptr; for (int &num : nums) { if (a == nullptr || num > *a) { c = b; b = a; a = # } elseif (*a > num && (b == nullptr || num > *b)) { c = b; b = # } elseif (b != nullptr && *b > num && (c == nullptr || num > *c)) { c = # } } return c == nullptr ? *a : *c; } };
[sol4-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { publicintthirdMax(int[] nums) { Integera=null, b = null, c = null; for (int num : nums) { if (a == null || num > a) { c = b; b = a; a = num; } elseif (a > num && (b == null || num > b)) { c = b; b = num; } elseif (b != null && b > num && (c == null || num > c)) { c = num; } } return c == null ? a : c; } }
[sol4-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicclassSolution { publicintThirdMax(int[] nums) { int? a = null, b = null, c = null; foreach (int num in nums) { if (a == null || num > a) { c = b; b = a; a = num; } elseif (a > num && (b == null || num > b)) { c = b; b = num; } elseif (b != null && b > num && (c == null || num > c)) { c = num; } } return c == null ? (int) a : (int) c; } }
[sol4-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
functhirdMax(nums []int)int { var a, b, c *int for _, num := range nums { num := num if a == nil || num > *a { a, b, c = &num, a, b } elseif *a > num && (b == nil || num > *b) { b, c = &num, b } elseif b != nil && *b > num && (c == nil || num > *c) { c = &num } } if c == nil { return *a } return *c }
[sol4-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var thirdMax = function(nums) { let a = null, b = null, c = null; for (const num of nums) { if (a === null || num > a) { c = b; b = a; a = num; } elseif (a > num && (b === null || num > b)) { c = b; b = num; } elseif (b !== null && b > num && (c === null || num > c)) { c = num; } } return c === null ? a : c; };