标签:
把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
public class Solution {
public int GetUglyNumber_Solution(int index) {
if (index == 0) {
return 0;
}
if (index == 1) {
return 1;
}
int[] result = new int[index];
result[0] = 1;
int resultPoint = 1;
int point2 = 0;
int point3 = 0;
int point5 = 0;
while (resultPoint < index) {
result[resultPoint] = min(result[point2] * 2, result[point3] * 3, result[point5] * 5);
if (result[point2] * 2 == result[resultPoint]) {
point2++;
}
if (result[point3] * 3 == result[resultPoint]) {
point3++;
}
if (result[point5] * 5 == result[resultPoint]) {
point5++;
}
resultPoint++;
}
return result[resultPoint - 1];
}
public int min(int n1, int n2, int n3) {
int min = (n1 < n2) ? n1: n2;
return (min < n3) ? min: n3;
}
}
标签:
原文地址:http://www.cnblogs.com/rosending/p/5641693.html