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)的更多相关文章

  1. 264 Ugly Number II 丑数 II

    编写程序找第 n 个丑数.丑数就是只包含质因子 2, 3, 5 的正整数.例如, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 就是前10个丑数.注意:1. 1 一般也被当做丑数2. ...

  2. [LeetCode] 264. Ugly Number II 丑陋数 II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  3. [LeetCode] 264. Ugly Number II 丑陋数之二

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  4. Leetcode264. Ugly Number II丑数2

    编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 ...

  5. leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes

    263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...

  6. 剑指 Offer 34. 二叉树中和为某一值的路径 + 记录所有路径

    剑指 Offer 34. 二叉树中和为某一值的路径 Offer_34 题目详情 题解分析 本题是二叉树相关的题目,但是又和路径记录相关. 在记录路径时,可以使用一个栈来存储一条符合的路径,在回溯时将进 ...

  7. 剑指 Offer 34. 二叉树中和为某一值的路径

    剑指 Offer 34. 二叉树中和为某一值的路径 输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径.从树的根节点开始往下一直到叶节点所经过的节点形成一条路径. 示例: 给定如下 ...

  8. 【刷题-LeetCode】264. Ugly Number II

    Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...

  9. 【LeetCode】264. Ugly Number II

    Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...

随机推荐

  1. python+selenium之简单介绍继承

    python+selenium之简单介绍继承 一.此例简单的介绍一下继承 1.面向对象的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过继承机制.继承完全可以理解成类之间的类型和子类型 ...

  2. 转载 -- iOS中SDK的简单封装与使用

    一.功能总述 在博客开始的第一部分,我们先来看一下我们最终要实现的效果.下图中所表述的就是我们今天博客中要做的事情,下方的App One和App Two都植入了我们将要封装的LoginSDK, 两个A ...

  3. web版pdf在线阅读器

    近期论坛里有人提到了,在线pdf阅读器怎么做.我百度了一下,发现非常多人都非常懒.程序猿都非常懒吗? 答案是否定的. 为什么都不愿意去搜索一下呢.网上非常多答案的.我这里就列举一例.大家共勉. 看代码 ...

  4. ArcGIS GP应用-GP模型创建-缓冲区分析

    一.参考资料 http://www.cnblogs.com/HPhone/archive/2012/11/05/2755974.html 二.关键步骤 1.缓冲分析数据源可以选择:点.面.线 2.构建 ...

  5. 苹果发布【新开发语言】Swift

    Swift is a multi-tenant, highly scalable and durable object storage system that was designed to stor ...

  6. std::unique_lock

    /*与Mutex RAII相关,方便线程上锁,相比std::lock_guard提供了更好的上锁解锁控制,反正我是没看出来也是在构造时上锁,在析构时解锁,感觉和lock_gurad大差不差都是在线程函 ...

  7. linux如何查看端口是否被占用?

    转自:https://www.cnblogs.com/hindy/p/7249234.html LINUX中如何查看某个端口是否被占用 之前查询端口是否被占用一直搞不明白,问了好多人,终于搞懂了,现在 ...

  8. [LintCode] 寻找缺失的数

    class Solution { public: /** * @param nums: a vector of integers * @return: an integer */ int findMi ...

  9. gridView在view页面中的一些代码详细模板

    <?php use yii\helpers\Html; use yii\grid\GridView; use yii\widgets\Pjax; use frontend\models\Item ...

  10. Quartz.net 基于配置的调度程序实践

    1.Nuget 搜索并安装Quartz.net 2.3.3  2.添加配置到App.config <?xml version="1.0" encoding="utf ...