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 ...
随机推荐
- MySQL导入sql脚本错误:2006 - MySQL server has gone away
到如一些小脚本很少报错,但最近导入一个10+M的SQL脚本,却重复报错: Error occured at:2014-03-24 11:42:24 Line no.:85 Error Code: 20 ...
- 046医疗项目-模块四:采购单模块—采购单审核(Dao,Service,Action三层)
当医院把采购单提交之后,由监管单位进行采购单审核,由卫生院及卫生局进行审核.卫生局可以审核所有医院创建的采购单,卫生院只审核本辖区医院创建的采购单. 操作流程: 点击"采购单审核" ...
- AngularJS中的digest循环$apply
欢迎大家指导与讨论 : ) 前言 Angular会拓展这个标准的浏览器流程,创建一个Angular上下文.这个Angular上下文指的是运行在Angular事件循环内的特定代码,该Angular事件循 ...
- ASP.NET MVC铵钮Click后下载文件
本篇Insus.NET练习的是FilePathResult和FileStreamResult操作.本篇也算是<如何把Json格式字符写进text文件中>http://www.cnblogs ...
- QT 对话框二
QMessageBox类 information()函数,主要是提示功能,不需要用户选择 StandardButton QMessageBox::information ( QWidget *pare ...
- 清北学堂2017NOIP冬令营入学测试P4747 D’s problem(d)
时间: 1000ms / 空间: 655360KiB / Java类名: Main 背景 冬令营入学测试题 描述 题目描述 小D是一名魔法师,它最喜欢干的事就是对批判记者了. 这次记者招待会上,记者对 ...
- 什么是API
我们从API的功能.分类.设计.实现.用户来看什么是API. API是应用程序组件之间通信的接口 --wiki:Application Programming Interface In compute ...
- Asp.net WebApi Put模式调用,“HTTP 错误 405.0 - Method Not Allowed”解决方法
IIS10.0在部署了WebAPI之后,默认是不支持Put模式调用的.需要按照下面方法启用. 步骤一:在IIS管理界面要支持Put模式的IIS站点,选择 "功能视图". 步骤二:选 ...
- python网络编程学习《一》
最近,刚实习完,很喜欢实验楼,但是自己的方向仍然不能确定,自己觉得可选择的空间很大,尽管已经是大四的人了,想到别人都在忙着买职业装,买高跟鞋面试,学习化妆什么的,看看自己,反而开始慢慢关注运动,食疗以 ...
- Android 开发1000问笔记
11.android使用全局变量 定义Data类继承Application 在manifest.xml中声明 http://blog.csdn.net/feiyangxiaomi/article/de ...