264. Ugly Number II

Posted boluo007

tags:

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

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

264. Ugly Number II

leetcode 264. Ugly Number II

LeetCode(264):Ugly Number II

264. Ugly Number II

leetcode 264: Ugly Number II

264. Ugly Number II