package java_study.JianZhiOffer;
import org.junit.Test;
/**
* Created by ethan on 2015/6/29.
* 剑指offer No34找到第1500个的丑数(因数只有2,3,5三个数为丑数)
* 空间换时间的做法
*/
public class No34找到丑数 {
public int findUglyNumber(int x){
int[] tmp = new int[x+1];
tmp[1] = 1;
int index_2 = 1;
int index_3 = 1;
int index_5 = 1;
int count=2;
while (count<=x){
int min = getMin(tmp[index_2]*2, tmp[index_3]*3, tmp[index_5]*5);
tmp[count++]=min;
while (tmp[index_2]*2<=min)
index_2++;
while (tmp[index_3]*3<=min)
index_3++;
while (tmp[index_5]*5<=min)
index_5++;
}
int ans = tmp[x];
tmp = null;
return ans;
}
public int getMin(int x, int y, int z){
int tmp = x<y?x:y;
return tmp<z?tmp:z;
}
@Test
public void test1(){
System.out.println(findUglyNumber(10));
}
}
找到第1500个的丑数
最新推荐文章于 2016-12-14 16:22:33 发布