To prove safety Offer 49

 1 # -*- coding:utf-8 -*-
 2 class Solution:
 3     def GetUglyNumber_Solution(self, index):
 4         if index <= 6:
 5             return index
 6         i2,i3,i5= 0,0,0
 7         dp = [0] * index
 8         dp[0] = 1
 9         for i in range(1,index):
10             next2 = dp[i2] * 2
11             next3 = dp[i3] * 3
12             next5 = dp[i5] * 5
13             dp[i] = min(next2,min(next3,next5))
14             if dp[i] == next2:
15                 i2 += 1
16             if dp[i] == next3:
17                 i3 += 1
18             if dp[i] == next5:
19                 i5 += 1
20         return dp[index-1]
21         # write code here

 

Guess you like

Origin www.cnblogs.com/asenyang/p/11022941.html