Description
Submission
class Solution {
public:
int nthUglyNumber(int n) {
int i = 0, j = 0, k = 0;
vector<int> rets{1};
for(int t = 0; t < n - 1; ++t) {
int cur = min(rets[i] * 2, min(rets[j] * 3, rets[k] * 5));
rets.push_back(cur);
if(cur == rets[i] * 2) ++i;
if(cur == rets[j] * 3) ++j;
if(cur == rets[k] * 5) ++k;
}
return rets.back();
}
};
class Solution {
typedef long long ll;
public:
int nthUglyNumber(int n) {
vector<ll> numbers;
vector<ll> factors{2, 3, 5};
set<ll> Set;
Set.insert(1);
for(int i = 0; i < n; ++i) {
int cur = *Set.begin();
Set.erase(Set.begin());
numbers.push_back(cur);
for(auto x: factors) {
Set.insert(x * cur);
}
}
return numbers[n-1];
}
};