(int[] nums)
| 1 | public class Solution { |
| 2 | public int thirdMax(int[] nums) { |
| 3 | PriorityQueue<Integer> pq = new PriorityQueue<>(3); |
| 4 | Set<Integer> set = new HashSet<>(); |
| 5 | for (int i : nums) { |
| 6 | if (set.contains(i)) continue; |
| 7 | pq.offer(i); |
| 8 | set.add(i); |
| 9 | if (pq.size() > 3) set.remove(pq.poll()); |
| 10 | } |
| 11 | while (pq.size() < 3 && pq.size() > 1) { |
| 12 | pq.poll(); |
| 13 | } |
| 14 | return pq.peek(); |
| 15 | } |
| 16 | } |