Write a program to check whether a given number is an ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.

Note that 1 is typically treated as an ugly number.

思路:如果num可以被2,3,5整除,那么就除以2,3,5.看最后是不是结果等于1.

  1. class Solution:
  2. # @param {integer} num
  3. # @return {boolean}
  4. def isUgly(self, num):
  5. if num <= 0:
  6. return False
  7. for x in [2, 3, 5]:
  8. while num % x == 0:
  9. num /= x
  10. return num == 1

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.

  1. class Solution(object):
  2. def nthUglyNumber(self, n):
  3. """
  4. :type n: int
  5. :rtype: int
  6. """
  7. res = [1]*n
  8. p2 = 0
  9. p3 = 0
  10. p5 = 0
  11.  
  12. count = 1
  13. while count < n:
  14. cur = min(res[p2]*2, res[p3]*3, res[p5]*5)
  15. if cur == res[p2]*2:
  16. p2 += 1
  17. elif cur == res[p3]*3:
  18. p3 += 1
  19. elif cur == res[p5]*5:
  20. p5 += 1
  21. if cur > res[count-1]:
  22. res[count] = cur
  23. count += 1
  24. return res[-1]

Leetcode 264. Ugly Number II的更多相关文章

  1. [leetcode] 264. Ugly Number II (medium)

    263. Ugly Number的子母题 题目要求输出从1开始数,第n个ugly number是什么并且输出. 一开始想着1遍历到n直接判断,超时了. class Solution { public: ...

  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. (medium)LeetCode 264.Ugly Number II

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

  5. LeetCode——264. Ugly Number II

    题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fact ...

  6. 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 ...

  7. 【LeetCode】264. Ugly Number II

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

  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 解题报告(Java & Python)

    标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...

随机推荐

  1. WPF 4.0 DatePicker 快速录入

    WPF 4.0的DatePicker在通过键盘录入日期的时候是非常让人郁闷的.必须按照日期的格式来完整输入例如,比如输入“2010/10/10”才能识别.而实际上在一些要求快速录入的场合,用户更希望直 ...

  2. Windows 8.1 新增控件之 CommandBar

    上一篇为大家介绍了AppBar 的相关内容,本篇继续介绍CommandBar 的使用方法.与AppBar 相比而言,CommandBar 在开发使用方面较为单一,在按键布局上分为主控区(Primary ...

  3. android 底层log分析 内存及backtrace tombstone/crash

    Build fingerprint: 'XXXXXXXXX'pid: 1658, tid: 13086  >>> system_server <<<signal 1 ...

  4. Android各种屏幕适配原理

    dip(dp): device independent pixels(设备独立像素) dip,就是把屏幕的高分成480分,宽分成320分.比如你做一条160dip的横线,无论你在320还480的模拟器 ...

  5. JDK动态代理和CGLib动态代理简单演示

    JDK1.3之后,Java提供了动态代理的技术,允许开发者在运行期间创建接口的代理实例. 一.首先我们进行JDK动态代理的演示. 现在我们有一个简单的业务接口Saying,如下: package te ...

  6. Datatable删除行的Delete和Remove方法

    在C#中,如果要删除DataTable中的某一行,大约有以下几种办法: 1,使用DataTable.Rows.Remove(DataRow),或者DataTable.Rows.RemoveAt(ind ...

  7. ASP.NET Web API Help Pages using Swagger

    Understanding the various methods of an API can be a challenge for a developer when building a consu ...

  8. BGP路由协议详解(完整篇)

    原文链接:http://xuanbo.blog.51cto.com/499334/465596/ 2010-12-27 12:02:45 上个月我写一篇关于BGP协议的博文,曾许诺过要完善这个文档,但 ...

  9. android第三方框架 xlistview 的使用

    如今上拉刷新,下拉加载更多已经是浩如烟海的app的标配了 最近正好有相关的需要就去学习了一下,还是那句老话凡事都靠自己来 搞实在不是一件好事,费时费力不说可能还是在做无用功,不过自己研究学习 还是很有 ...

  10. mybatis Generator配置文件详解

    这里按照配置的顺序对配置逐个讲解,更细的内容可以配合中文文档参照. 1. 配置文件头 <?xml version="1.0" encoding="UTF-8&quo ...