题目
分析
每个数乘以2,3,5 就会得到接下来的丑数,但是这个丑数不一定是正好可以排在后面的,所以有个采用不采用的情况。
如果p2,p3,p5三个指针某个指针指导的数被采用了,这个指针后移一位。
python代码
class Solution:
def GetUglyNumber_Solution(self, index):
# write code here
if index ==0: return 0
p2 = p3 = p5 = 0
res = [1]
i = 0
while i<index:
val = min(res[p2]*2,res[p3]*3,res[p5]*5)
res.append(val)
if res[p2]*2 ==val:
p2+=1
if res[p3]*3 ==val:
p3+=1
if res[p5]*5 ==val:
p5+=1
i+=1
return res[i-1]