题解 | #丑数#
丑数
http://www.nowcoder.com/practice/6aa9e04fc3794f68acf8778237ba065b
从这道题学习到的是: 如果知道一个数的质因子是a,b,c,那么这个数可以通过a^n1+b^n2+c^3来确定,这样我们就可以找到,所有质因子为a,b,c这样的数了。
public class Solution {
public int GetUglyNumber_Solution(int index) {
if(index <= 6) return index;
int i2 = 0, i3 = 0, i5 = 0;
int[] res = new int[index];
res[0] = 1;
for (int i = 1; i < index; i++) {
res[i] = Math.min(res[i2] * 2, Math.min(res[i3] * 3, res[i5] * 5));
if (res[i] == res[i2] * 2) i2++;
if (res[i] == res[i3] * 3) i3++;
if (res[i] == res[i5] * 5) i5++;
}
return res[index - 1];
}
}