我们把只包含质因子 2、3 和 5 的数称作丑数(Ugly Number)。求按从小到大的顺序的第 n 个丑数。
相关链接:
力扣
2、解题代码及当时思考:public class NumberOfUgly {
public int nthUglyNumber(int n) {
int[] dp = new int[n];
int xIndex = 0;
int yIndex = 0;
int zIndex = 0;
dp[0] = 1;
for (int i = 1; i < n; i++) {
int xValue = dp[xIndex] * 2;
int yValue = dp[yIndex] * 3;
int zValue = dp[zIndex] * 5;
dp[i] = Math.min(Math.min(xValue, yValue), zValue);
if (xValue == dp[i]) {
xIndex++;
}
if (yValue == dp[i]) {
yIndex++;
}
if (zValue == dp[i]) {
zIndex++;
}
}
return dp[n-1];
}
private boolean isUgly(int num) {
while (num % 2 == 0) {
num = num / 2;
}
while (num % 5 == 0) {
num = num / 5;
}
while (num % 3 == 0) {
num = num / 3;
}
return num == 1;
}
public static void main(String[] args) {
NumberOfUgly numberOfUgly = new NumberOfUgly();
System.out.println(numberOfUgly.nthUglyNumber(10));
}
}



