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

Posted 张乐乐章

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了264. Ugly Number II(丑数 剑指offer 34)相关的知识,希望对你有一定的参考价值。

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)的主要内容,如果未能解决你的问题,请参考以下文章

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

LeetCode——264. 丑数 II

LeetCode 264

LeetCode 0264. 丑数 II

264. Ugly Number II

leetcode 264. Ugly Number II