题目描述
把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
class Solution:def GetUglyNumber_Solution(self, index):# write code hereif index == 0:return 0dp = [0] * indexdp[0] = 1a,b,c = 0,0,0for i in range(1,index):n2,n3,n5 = dp[a]*2,dp[b]*3,dp[c]*5dp[i] = min(n2,n3,n5)if dp[i] == n2: a+=1if dp[i] == n3: b+=1if dp[i] == n5: c+=1return dp[index-1]
