zoukankan      html  css  js  c++  java
  • leetcode刷题笔记 264题 丑数 II

    leetcode刷题笔记 264题 丑数 II

    源地址:264. 丑数 II

    问题描述:

    编写一个程序,找出第 n 个丑数。

    丑数就是质因数只包含 2, 3, 5 的正整数。

    示例:

    输入: n = 10
    输出: 12
    解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。
    说明:

    1 是丑数。
    n 不超过1690。

    //通过分别构建2为基础的丑数集合,3为基础的丑数集合,5为基础的丑数集合,进行多路归并排序
    import scala.collection.mutable
    object Solution {
        def nthUglyNumber(n: Int): Int = {
            val nums = mutable.ListBuffer[Int]()
            nums.append(1)
            
            var i = 0
            var j = 0
            var k = 0
    
            while (nums.length < n) {
                val temp = math.min(nums(i)*2, math.min(nums(j)*3, nums(k)*5))
                if (nums(i)*2 == temp) i += 1
                if (nums(j)*3 == temp) j += 1
                if (nums(k)*5 == temp) k += 1
                nums.append(temp)
            }
            //println(nums.mkString(" "))
            return nums.last
        }
    }
    
  • 相关阅读:
    flex-direction
    flex-grow
    Push API
    ServiceWorker.state
    Using Service Workers
    Promise.then
    Promise()
    Using promises
    node-rsa
    async.waterfall
  • 原文地址:https://www.cnblogs.com/ganshuoos/p/13930874.html
Copyright © 2011-2022 走看看