码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-03-11 17:27:18      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:ica   ==   who   question   enc   bsp   span   des   def   

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)

标签:ica   ==   who   question   enc   bsp   span   des   def   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!