丑数的核心思想是:用已经生成的丑数chen乘2、3、5,求其最小值为新的丑数。
int nthUglyNumber(int n) {
// write your code here
vector<int> count(1,1);
int i2 = 0, i3 = 0, i5 = 0;
while (count.size() < n)
{
int n2 = count[i2] * 2;
int n3 = count[i3] * 3;
int n5 = count[i5] * 5;
int next = min(n2, min(n3, n5));
if (next == n2)
++i2;
if (next == n3)
++i3;
if (next == n5)
++i5;
count.push_back(next);
}
return count.back();
}