Third Maximum Number(C++第三大的数)

解题思路:

(1)使用优先队列排序,依次出队列,取前面的第三大不同数

class Solution {
public:
    int thirdMax(vector<int>& nums) {
    	
        priority_queue<int> pq;
        for (auto w:nums) {
        	pq.push(w);
		}
		
		int max = pq.top(),maxx = pq.top();
		int count = 1;
		pq.top();
		while(!pq.empty()) {
			if (max != pq.top()) {
				count++;
				if (count==3) return pq.top();
				max = pq.top();
			}
			pq.pop();
		}
        
		return maxx;
    }
};
发布了264 篇原创文章 · 获赞 272 · 访问量 42万+

猜你喜欢

转载自blog.csdn.net/coolsunxu/article/details/105473966