class Solution {
public:
long nthUglyNumber(int n) {
set<long> pq;
pq.insert(1);
int count = 0;
long t = 1;
while (count < n)
{
set<long>::iterator it = pq.begin();
t = *it;
pq.erase(it);
pq.insert(t * 2);
pq.insert(t * 3);
pq.insert(t * 5);
count++;
}
return t;
}
};
My solution in c++

Nice idea to use a set. And I now know that
long
already has 64 bits here, so I can stop usinglong long
:)I just shortened a few things:
int nthUglyNumber(int n) { set<long> pq {1}; long t; while (n) { auto it = pq.begin(); t = *it; pq.erase(it); pq.insert(t * 2); pq.insert(t * 3); pq.insert(t * 5); } return t; }