【剑指offer】丑数(python)
我们只用比较3个数:用于乘2的最小的数、用于乘3的最小的数,用于乘5的最小的。
# -*- coding:utf-8 -*- class Solution: def GetUglyNumber_Solution(self, index): # write code here if index <= 6: return index i2 = 0 i3 = 0 i5 = 0 dp = [0 for i in range(index)] dp[0] = 1 for i in range(1, index): next2 = dp[i2]*2 next3 = dp[i3]*3 next5 = dp[i5]*5 dp[i] = min(next2,min(next3,next5)) if dp[i] == next2: i2+=1 if dp[i] == next3: i3+=1 if dp[i] == next5: i5+=1 return dp[index-1]