这个题我没有坐上来。是看别人的思路写的
public class Solution {
public int nthUglyNumber(int n) {
List<Integer> table = new ArrayList<Integer>();
table.add(1);
int index2=0,index3=0,index5=0;
for(int i=1;i<n;i++)
{
int next=Math.min(Math.min( table.get(index2)*2,table.get(index3)*3),table.get(index5)*5);
if(next==table.get(index2)*2)
{
index2++;
}
if(next==table.get(index3)*3)
{
index3++;
}
if(next==table.get(index5)*5)
{
index5++;
}
table.add(next);
}
return table.get(n-1);
}
}
本文介绍了一个使用Java编写的算法,用于找出第N个丑数。丑数是指只包含质因数2、3和5的正整数。通过维护三个指针并利用动态规划的思想,该算法能有效地生成指定位置的丑数。
726

被折叠的 条评论
为什么被折叠?



