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

264. Ugly Number II

时间:2020-03-21 19:41:35      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:include   div   not   enc   prim   number   bsp   numbers   nbsp   

Write a program to find the n-th ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5

Example:

Input: n = 10
Output: 12
Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.

Note:  

  1. 1 is typically treated as an ugly number.
  2. n does not exceed 1690.

 

class Solution(object):
    def nthUglyNumber(self, n):
        """
        :type n: int
        :rtype: int
        """
        nums = [1]
        i = 0
        j = 0
        k = 0
        
        for _ in range(n-1):
            ugly = min(nums[i]*2, nums[j]*3, nums[k]*5)
            nums.append(ugly)
            
            if ugly == nums[i]*2:
                i += 1
            if ugly == nums[j]*3:
                j += 1
            if ugly == nums[k]*5:
                k += 1
        
        return nums[n-1]
            
            

 

264. Ugly Number II

标签:include   div   not   enc   prim   number   bsp   numbers   nbsp   

原文地址:https://www.cnblogs.com/boluo007/p/12541567.html

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