/*
* @lc app=leetcode.cn id=263 lang=c
*
* [263] 丑数
*
* https://leetcode-cn.com/problems/ugly-number/description/
*
* algorithms
* Easy (44.82%)
* Total Accepted: 7K
* Total Submissions: 15.7K
* Testcase Example: '6'
*
* 编写一个程序判断给定的数是否为丑数。
*
* 丑数就是只包含质因数 2, 3, 5 的正整数。
*
* 示例 1:
*
* 输入: 6
* 输出: true
* 解释: 6 = 2 × 3
*
* 示例 2:
*
* 输入: 8
* 输出: true
* 解释: 8 = 2 × 2 × 2
*
*
* 示例 3:
*
* 输入: 14
* 输出: false
* 解释: 14 不是丑数,因为它包含了另外一个质因数 7。
*
* 说明:
*
*
* 1 是丑数。
* 输入不会超过 32 位有符号整数的范围: [−2^31,  2^31 − 1]。
*
*
*/
bool isUgly(int num) {
if (num <= ){
return false;
}
//如果5是当前num的因子
while (num % == ) {
num /= ;
}
//如果3是当前num的因子
while (num % == ) {
num /= ;
}
//如果2是当前num的因子
while (num % == ) {
num /= ;
}
return num == ;
}

其实就是如果有2,3,5的因子就一直分解下去。最后如果分解到1的话那么他就是丑数,否则不是。

---------------------------------------------------------------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=263 lang=python3
#
# [263] 丑数
#
# https://leetcode-cn.com/problems/ugly-number/description/
#
# algorithms
# Easy (44.82%)
# Total Accepted: 7K
# Total Submissions: 15.7K
# Testcase Example: '6'
#
# 编写一个程序判断给定的数是否为丑数。
#
# 丑数就是只包含质因数 2, 3, 5 的正整数。
#
# 示例 1:
#
# 输入: 6
# 输出: true
# 解释: 6 = 2 × 3
#
# 示例 2:
#
# 输入: 8
# 输出: true
# 解释: 8 = 2 × 2 × 2
#
#
# 示例 3:
#
# 输入: 14
# 输出: false
# 解释: 14 不是丑数,因为它包含了另外一个质因数 7。
#
# 说明:
#
#
# 1 是丑数。
# 输入不会超过 32 位有符号整数的范围: [−2^31,  2^31 − 1]。
#
#
#
class Solution:
def isUgly(self, num: int) -> bool:
for p in 2, 3, 5:
while num % p == 0 < num:
num /= p
return num == 1

Leecode刷题之旅-C语言/python-263丑数的更多相关文章

  1. Leecode刷题之旅-C语言/python-9.回文数

    /* * @lc app=leetcode.cn id=9 lang=c * * [9] 回文数 * * https://leetcode-cn.com/problems/palindrome-num ...

  2. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  3. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  4. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  5. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  6. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  7. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  8. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

  9. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

随机推荐

  1. 外部读取Excel的两种方法

    1.使用Epplus读取 下载地址为https://epplus.codeplex.com/,下载文件后引用Epplus.dll文件. 这个类库读取Excel方便快捷,但是它只能读取.xlsx类型的文 ...

  2. Python学习---Django的request扩展[获取用户设备信息]

    关于Django的request扩展[获取用户设备信息] settings.py INSTALLED_APPS = [ ... 'app01', # 注册app ] STATICFILES_DIRS ...

  3. 3D旋转相册的实现

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. July 28th 2017 Week 30th Friday

    If equal affection cannot be, let the more loving be me. 如果没有相等的爱,那就让我爱多一点吧. There is seldom equal a ...

  5. AngularCLI介绍及配置文件主要参数含义解析

    使用Angular CLI可以快速,简单的搭建一个angular2或angular4项目,是只要掌握几行命令就能构建出前端架构的最佳实践,它本质也是使用了webpack来编译,打包,压缩等构建的事情, ...

  6. 【[CQOI2016]手机号码】

    递推版的数位dp 绝对的暴力美学 我们设\(dp[l][i][j][0/1][0/1][0/1]\)表示到了第\(l\)位,这一位上选择的数是\(i\),\(l-1\)位选择的数是\(j\),第一个\ ...

  7. ethereumjs/ethereumjs-vm-2-API文档

    https://github.com/ethereumjs/ethereumjs-vm/blob/master/docs/index.md vm.runBlockchain Processes blo ...

  8. 转载:monkeyrunner之eclipse中运行monkeyrunner脚本之环境搭建(四)

    转载自:lynnLi 的monkeyrunner之eclipse中运行monkeyrunner脚本之环境搭建(四) monkeyrunner脚本使用Python语法编写,但它实际上是通过Jython来 ...

  9. mean函数一个神代码

    cs231n上的一个代码,能求出准确率: print 'accuracy: %f' % ( np.mean(Yte_predict == Yte) ) >>> a = np.arra ...

  10. springboot项目用maven打jar包

    clean package -Dmaven.test.skip=true idea eclipse STS