丑数:因子只包含2,3,5的数,1为第一个丑数,2,3,5皆为丑数,请第1500个丑数。
public class Offer55 {
public static void main(String[] args) {
System.out.println(new Offer55().getUglyNumber_solution(5));
}
public int getUglyNumber_solution(int index){
if(index<=0)return 0;
int[] temp = new int[index];
temp[0]=1;
int towIndex = 0;
int threeIndex = 0;
int fiveIndex = 0;
for(int i=1;i<index;i++){
int min = Math.min(Math.min(temp[towIndex]*2,temp[threeIndex]*3),temp[fiveIndex]*5);
temp[i] = min;
if(temp[i] == temp[towIndex]*2)towIndex++;
if(temp[i] == temp[threeIndex]*3)threeIndex++;
if(temp[i] == temp[fiveIndex]*5)fiveIndex++;
}
return temp[index-1];
}
}
还没有评论,来说两句吧...