cpp
class Solution {
public:
long long ret =INT_MAX;
vector<int> v = { 2,3,5 };
void dfs(long long tar, int idx, long long last)
{
if (tar > last)
{
if (ret == last)
ret = tar;
else ret = min(ret, tar);
return;
}
for (int i = idx; i < 3; i++)
{
tar *= v[i];
dfs(tar, i, last);
tar /= v[i];
}
}
int nthUglyNumber(int n)
{
long long last = 1;
for (int i = 2; i <= n; i++)
{
dfs(1, 0, last);
last = ret;
}
return ret == INT_MAX? 1 : ret;
}
};
自己的方法,不是最优解。