思路:设定初始丑数列表,每次寻找最近的一个丑数。
由于丑数的因子只能是2,3,5,所以需要将list中所有已知丑数乘以2,3,5,然后所有候选中大于当前最大丑数的最小丑数就是下一个丑数。
为了提高效率,被判断出已经在当前列表的元素,不需要重复计算。
import java.util.*;
public class Solution {
public int minNumber(int a,int b){
return a<=b?a:b;
}
public int GetUglyNumber_Solution(int index) {
if(index<=0) return 0;
//初始化丑数存储list
index-=1;
ArrayList<Integer> UglyNumbers=new ArrayList<>();
UglyNumbers.add(1);
UglyNumbers.add(2);
UglyNumbers.add(3);
UglyNumbers.add(4);
UglyNumbers.add(5);
int length=UglyNumbers.size();
//初始化几个参数
int M2index=0,M3index=0,M5index=0;
int M2=0,M3=0,M5=0;
int maxUglyNum=5,nextUglyNum=0;
//begin
if(index<length) return UglyNumbers.get(index);
while(length-1<index){
//乘2的丑数候选处理
while(M2index<length&&M2<=maxUglyNum){
M2=2*UglyNumbers.get(M2index++);
}
if(M2index<length) M2index--;
else return 0;//异常返回
//乘3的丑数候选处理
while(M3index<length&&M3<=maxUglyNum){
M3=3*UglyNumbers.get(M3index++);
}
if(M3index<length) M3index--;
else return 0;//异常返回
//乘5的丑数候选处理
while(M5index<length&&M5<=maxUglyNum){
M5=5*UglyNumbers.get(M5index++);
}
if(M5index<length) M5index--;
else return 0;//异常返回
//获得下一个丑数
nextUglyNum=minNumber(minNumber(M2,M3),M5);
//三个指针分别移动
if(nextUglyNum==M2){
M2index++;
}
if(nextUglyNum==M3){
M3index++;
}
if(nextUglyNum==M5){
M5index++;
}
//更新list
UglyNumbers.add(nextUglyNum);
length=UglyNumbers.size();
maxUglyNum=nextUglyNum;
M2=0;M3=0;M5=0;
}
return nextUglyNum;
}
}