题解 | #丑数#

丑数

https://www.nowcoder.com/practice/6aa9e04fc3794f68acf8778237ba065b

#include <functional>
#include <queue>
#include <unordered_map>
class Solution {
public:
    int GetUglyNumber_Solution(int index) {
        if(index==0){
            return 0;
        }
        vector<int> vec={2,3,5};
        unordered_map<long, int> map;
        priority_queue<long,vector<long>,greater<long>> ans;
        map[1]=1;
        ans.push(1);
        long a=0;
        for(int i=0;i<index;i++){
            a=ans.top();
            ans.pop();
            for(int j=0;j<3;j++){
                long t=a*vec[j];
                if(map.find(t)==map.end()){
                    map[t]=1;
                    ans.push(t);
                }
            }
        }
        return a;
    }
};

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-29 14:02
纠结的茶叶蛋在开会:太简单了,技术和业务没一个够用的,要么学业务,去搞erp或者his,要么专精技术,去搞架构
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务