zoukankan      html  css  js  c++  java
  • 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]
  • 相关阅读:
    day66
    1
    day65
    BeautifulSoup
    day60
    day59
    day49
    day48
    [S5PV210] PWM
    [S5PV210] Clock
  • 原文地址:https://www.cnblogs.com/zle1992/p/8543886.html
Copyright © 2011-2022 走看看