从前往后构造:
ugl[1] = 1;
之后的丑数肯定是之前的丑数的2 3 5 倍;
因此 :
class Solution {
public:
int ugl[50000], cnt = 1;
int getUglyNumber(int n) {
ugl[1] = 1;
for (int i = 2; i <= n; i ++ ) {
ugl[i] = 1e9;
for (int j = 1; j <= cnt; j ++ ) {
if (ugl[i] > ugl[j] * 2 && ugl[j] * 2 > ugl[cnt]) ugl[i] = ugl[j] * 2;
if (ugl[i] > ugl[j] * 3 && ugl[j] * 3 > ugl[cnt]) ugl[i] = ugl[j] * 3;
if (ugl[i] > ugl[j] * 5 && ugl[j] * 5 > ugl[cnt]) ugl[i] = ugl[j] * 5;
}
cnt ++ ;
}
return ugl[n];
}
};