Leetcode 264. Ugly Number II
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.
- class Solution:
- # @param {integer} num
- # @return {boolean}
- def isUgly(self, num):
- if num <= 0:
- return False
- for x in [2, 3, 5]:
- while num % x == 0:
- num /= x
- 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.
- class Solution(object):
- def nthUglyNumber(self, n):
- """
- :type n: int
- :rtype: int
- """
- res = [1]*n
- p2 = 0
- p3 = 0
- p5 = 0
- count = 1
- while count < n:
- cur = min(res[p2]*2, res[p3]*3, res[p5]*5)
- if cur == res[p2]*2:
- p2 += 1
- elif cur == res[p3]*3:
- p3 += 1
- elif cur == res[p5]*5:
- p5 += 1
- if cur > res[count-1]:
- res[count] = cur
- count += 1
- return res[-1]
Leetcode 264. Ugly Number II的更多相关文章
- [leetcode] 264. Ugly Number II (medium)
263. Ugly Number的子母题 题目要求输出从1开始数,第n个ugly number是什么并且输出. 一开始想着1遍历到n直接判断,超时了. class Solution { public: ...
- [LeetCode] 264. Ugly Number II 丑陋数 II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] 264. Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- (medium)LeetCode 264.Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- LeetCode——264. Ugly Number II
题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fact ...
- 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 ...
- 【LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- 【刷题-LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- 【LeetCode】264. Ugly Number II 解题报告(Java & Python)
标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...
随机推荐
- WPF 4.0 DatePicker 快速录入
WPF 4.0的DatePicker在通过键盘录入日期的时候是非常让人郁闷的.必须按照日期的格式来完整输入例如,比如输入“2010/10/10”才能识别.而实际上在一些要求快速录入的场合,用户更希望直 ...
- Windows 8.1 新增控件之 CommandBar
上一篇为大家介绍了AppBar 的相关内容,本篇继续介绍CommandBar 的使用方法.与AppBar 相比而言,CommandBar 在开发使用方面较为单一,在按键布局上分为主控区(Primary ...
- android 底层log分析 内存及backtrace tombstone/crash
Build fingerprint: 'XXXXXXXXX'pid: 1658, tid: 13086 >>> system_server <<<signal 1 ...
- Android各种屏幕适配原理
dip(dp): device independent pixels(设备独立像素) dip,就是把屏幕的高分成480分,宽分成320分.比如你做一条160dip的横线,无论你在320还480的模拟器 ...
- JDK动态代理和CGLib动态代理简单演示
JDK1.3之后,Java提供了动态代理的技术,允许开发者在运行期间创建接口的代理实例. 一.首先我们进行JDK动态代理的演示. 现在我们有一个简单的业务接口Saying,如下: package te ...
- Datatable删除行的Delete和Remove方法
在C#中,如果要删除DataTable中的某一行,大约有以下几种办法: 1,使用DataTable.Rows.Remove(DataRow),或者DataTable.Rows.RemoveAt(ind ...
- 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 ...
- BGP路由协议详解(完整篇)
原文链接:http://xuanbo.blog.51cto.com/499334/465596/ 2010-12-27 12:02:45 上个月我写一篇关于BGP协议的博文,曾许诺过要完善这个文档,但 ...
- android第三方框架 xlistview 的使用
如今上拉刷新,下拉加载更多已经是浩如烟海的app的标配了 最近正好有相关的需要就去学习了一下,还是那句老话凡事都靠自己来 搞实在不是一件好事,费时费力不说可能还是在做无用功,不过自己研究学习 还是很有 ...
- mybatis Generator配置文件详解
这里按照配置的顺序对配置逐个讲解,更细的内容可以配合中文文档参照. 1. 配置文件头 <?xml version="1.0" encoding="UTF-8&quo ...