不多说了,直接上代码。
public class UglyNumberII {
public int nthUglyNumber(int n) {
List<Integer> ls = new ArrayList<Integer>();
ls.add(1);
int count2 = 0,count3 = 0,count5 = 0;
while(ls.size() < n){
//其实是3个指针(count2,count3,count5分别对应ls的下标)
int x = ls.get(count2) * 2;
int y = ls.get(count3) * 3;
int z = ls.get(count5) * 5;
int temp = Math.min(Math.min(x, y), z);
ls.add(temp);
//如果最小值是*2得到,则将count2指针后移
if(temp == x){
count2 ++;
}
//如果最小值是*3得到,则将count3指针后移
if(temp == y){
count3 ++;
}
//如果最小值是*5得到,则将count5指针后移
if(temp == z){
count5 ++;
}
}
return ls.get(ls.size() - 1);
}
}