丑数

求第n个丑数:

性质:1是丑数,如果一个数x是丑数,那么2x, 3x, 5x都是丑数,需要注意的是一个丑数可能由多种方式生成

 1 typedef long long ll;
 2 class Solution {
 3 public:
 4     int nthUglyNumber(int n) {
 5         vector<ll> num = {2, 3, 5};
 6         set<ll> s;
 7         priority_queue<ll, vector<ll>, greater<ll> > pq;
 8         s.insert(1);
 9         pq.push(1);
10         for(int i = 1;;++i){
11             auto t = pq.top();pq.pop();
12             if(i == n)return t;
13             for(auto p : num){
14                 auto a = p * t;
15                 if(!s.count(a)){
16                     pq.push(a);
17                     s.insert(a);
18                 }
19             }
20         }
21         return 0;
22     }
23 };
View Code
原文地址:https://www.cnblogs.com/sxq-study/p/12262208.html