首页 > 其他 > 详细

264. Ugly Number II(丑数 剑指offer 34)

时间:2018-03-11 16:34:28      阅读:232      评论:0      收藏:0      [点我收藏+]
Write a program to find the n-th ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.

Note that 1 is typically treated as an ugly number, and n does not exceed 1690.

 

 1 class Solution:
 2 
 3     def nthUglyNumber(self, n):
 4         """
 5         :type n: int
 6         :rtype: int
 7         """
 8         l= []
 9         l.append(1)
10         t2,t3,t5=0,0,0
11         mins = 0
12         while len(l)<n:
13             mins = min(l[t2]*2,l[t3]*3,l[t5]*5)
14             l.append(mins)
15             if(mins == l[t2]*2):
16                 t2+=1
17             if(mins == l[t3]*3):
18                 t3+=1
19             if(mins == l[t5]*5):
20                 t5+=1
21         return l[-1]

 

264. Ugly Number II(丑数 剑指offer 34)

原文:https://www.cnblogs.com/zle1992/p/8543886.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!