/*
* @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. 海量数据处理面试题(2) 将用户的query按出现频度排序

    问题描述: 有10个文件,每个文件1G,每个文件的每一行存放的都是用户的query,每个文件的query都可能重复.要求你按照query的频度排序. 分析:一般海量数据采用分治法时,都要用到哈希,将相 ...

  2. Java学习---Pinyin4j使用手册

    一般用法 pinyin4j的使用很方便,一般转换只需要使用PinyinHelper类的静态工具方法即可: String[] pinyin = PinyinHelper.toHanyuPinyinStr ...

  3. Mysql学习---使用Python执行存储过程

    使用Python执行存储过程 使用Python执行存储过程[2部分]: 1.执行存储过程,获取存储过程的结果集  2.将返回值设置给了  @_存储过程名_序号 = #!/usr/bin/env pyt ...

  4. 《C++ Primer Plus》读书笔记之五—函数-C++的编程模块

    函数-C++的编程模块   1.C++对于返回值的类型有一定的限制:不能是数组,但可以是其他任何类型——整数.浮点数.指针,甚至可以是结构和对象(有趣的是,虽然C++函数不能直接返回数组,但可以将数组 ...

  5. linux man指令问题

    linux man指令问题 2010-1-13 13:33 提问者: 钟离伊轩 man命令执行时,可加入数值,来限制帮助级别. 这句话对不对啊???? 我记得man page是分章节的..好像可以加数 ...

  6. 015.2Condiction接口

    Condiction对象能够让线程等待,也能够唤醒相应的线程,通过下面方法,具体看代码:await();signal();signalAll(); 使用步骤:1)创建锁2)通过锁拿到Condictio ...

  7. BZOJ 1295 最长距离 BFS+枚举

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1295 题目大意: windy有一块矩形土地,被分为 N*M 块 1*1 的小格子. 有 ...

  8. Java并发案例05---Master-Worker模式

    Master-Worker 模式是常用的并行计算模式.它的核心思想是系统由两类进程协同工作,Master和Worker进程.Master负责接收和分配任务,Worker负责处理子任务.当各个Worke ...

  9. YourUninstaller注册码(可用)

    Name:Giveawayoftheday Registration code: 000017-2PNBK2-J59U6F-317E09-R5TGJQ-6B1WNA-AZCYNJ-GVP86A-7VP ...

  10. js尾巴

    js中根据id获取标签: /** * 根据id获取标签 * @param {string}id * @returns {object} */ function $(id) { return typeo ...