# 三指针 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param index int整型 # @return int整型 # classSolution: defGetUglyNumber_Solution(self , index: int) -> int: if (index <= 6): return index res = [] res.append(1) i2 = 0 i3 = 0 i5 = 0 for i inrange(index): res.append(min(res[i2]*2,min(res[i3]*3,res[i5]*5))) if(res[-1]==res[i2]*2): i2 += 1 if(res[-1]==res[i3]*3): i3 += 1 if(res[-1]==res[i5]*5): i5 += 1 return res[index-1]
1 2 3 4 5 6 7 8
# 打表 但是我不确定怎么找到的30,20,15 # 看了评论区本人的回答,居然是试出来的... # 打表是对的 defGetUglyNumber_Solution(self, index): res=[2**i*3**j*5**k for i inrange(30) for j inrange(20) for k inrange(15)] res.sort() return res[index-1] if index else0