原题链接:https://leetcode.com/problems/ugly-number-ii/
Description
Write a program to find the nth ugly number.
Ugly numbers are positive numbers whose prime factors only include 2,3,5. For example, 1,2,3,4,5,6,8,9,10,12 ,is the sequence of the first 10 ugly numbers.
Note that 1 is typically treated as an ugly number.
class Solution {
private:
typedef unsigned long long int ull;
struct cmp {
bool operator()(const ull a, const ull b) {
return a > b;
}
};
public:
int nthUglyNumber(int n) {
ull lb = 0, ub = 0, arr[3] = { 2, 3, 5 };
priority_queue<ull, vector<ull>, cmp > q;
q.push(1);
for (int i = 1; i <= n;) {
ull val = q.top(); q.pop();
ub = val;
if (ub == lb) {
continue;
} else {
lb = val;
i++;
}
for (int j = 0; j < 3; j++) q.push(val * arr[j]);
}
return (int)lb;
}
};