丑数

class Solution {
public:
    int res;
    priority_queue<int,vector<int>,greater<int>> minheap;
    unordered_set<int> s;//所有出现过的丑数放在集合中
    int getUglyNumber(int n) {
        minheap.push(1);
        while(s.size()<n)
        {
            res=minheap.top();minheap.pop();
            if(s.count(res))  continue;//第一次遇到一个丑数时才拓展
            s.insert(res);
            minheap.push(res*2);
            minheap.push(res*3);
            minheap.push(res*5);
        }
        return res;
    }
};
posted @   穿过雾的阴霾  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示