Description
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).
1 | Example 1: |
1 | Example 2: |
1 | Example 3: |
Explanation: Note that the third maximum here means the third maximum distinct number.
Both numbers with value 2 are both considered as second maximum.
Method
先将数组进行排序,排序的复杂度是O(n)。然后从大数开始往Set里面加,当Set的长度为3时,返回这个数。如果没有长度为3的情况,则返回最大的数。
Solution
1 | public class Solution { |