Skip to content

Instantly share code, notes, and snippets.

@lishunan246
Created April 11, 2021 07:44
Show Gist options
  • Save lishunan246/a86966dc1b3007428d34b4f08a675a3c to your computer and use it in GitHub Desktop.
Save lishunan246/a86966dc1b3007428d34b4f08a675a3c to your computer and use it in GitHub Desktop.
Ugly Number II
class Solution {
public:
int nthUglyNumber(int n) {
std::vector<int> result;
result.push_back(1);
int p2 = 0;
int p3 = 0;
int p5 = 0;
while (result.size() < n) {
int x = std::min({result[p2] * 2, result[p3] * 3, result[p5] * 5});
result.push_back(x);
if (x == result[p2] * 2) {
p2++;
}
if (x == result[p3] * 3) {
p3++;
}
if (x == result[p5] * 5) {
p5++;
}
}
return result[n - 1];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment