Created
April 11, 2021 07:44
-
-
Save lishunan246/a86966dc1b3007428d34b4f08a675a3c to your computer and use it in GitHub Desktop.
Ugly Number II
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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