Class Solution

java.lang.Object
g0401_0500.s0414_third_maximum_number.Solution

public class Solution extends Object
414 - Third Maximum Number.<p>Easy</p> <p>Given an integer array <code>nums</code>, return <em>the <strong>third distinct maximum</strong> number in this array. If the third maximum does not exist, return the <strong>maximum</strong> number</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [3,2,1]</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> The first distinct maximum is 3. The second distinct maximum is 2. The third distinct maximum is 1.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,2]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> The first distinct maximum is 2. The second distinct maximum is 1. The third distinct maximum does not exist, so the maximum (2) is returned instead.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [2,2,3,1]</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> The first distinct maximum is 3. The second distinct maximum is 2 (both 2&rsquo;s are counted together since they have the same value). The third distinct maximum is 1.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 10<sup>4</sup></code></li> <li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li> </ul> <p><strong>Follow up:</strong> Can you find an <code>O(n)</code> solution?</p>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • thirdMax

      public int thirdMax(int[] nums)