/*
* @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. 修改UIView的backedlayer为CAShapeLayer

    修改UIView的backedlayer为CAShapeLayer 什么叫backedlayer呢?backedlayer指的是一个View在创建好的时候就已经帮你创建好了一个与View大小一致的CA ...

  2. December 28th 2016 Week 53rd Wednesday

    Knowledge is a treasure, but practice is the key to it. 知识是珍宝,而实践是获取她的钥匙. I know a lot, but what I r ...

  3. 021.12 IO流 ObjectStream对象序列化

    内容:通过文件存储对象我们遇到的问题,需要保存对象到硬盘中,如何解决这个就是用来解决的 用法:1.创建流对象FileOutputstream2.创建ObjectOutputStream对象与FileO ...

  4. lisp base

    一 .quote lisp 使用s-expr表示数据和代码,通常会将第一项作为函数,而将后续元素当做参数传给第一项进行计算.可以通过quote来进行其他解析,quote可用(‘)表示: ( + 1 1 ...

  5. 【[CQOI2018]交错序列】

    这个题简直有毒,\(O((a+b)^3logn)\)的做法不卡常只比\(O(2^n*n)\)多\(10\)分 看到\(a\)和\(b\)简直小的可怜,于是可以往矩阵上联想 发现这个柿子有些特殊,好像可 ...

  6. Android SDK Manager仅有一个版本的问题

    搭建好MonkeyRunner的环境之后,建立虚拟器的时候发现SDK的管理器中只有4.3的版本,查阅了一下百度,问题解决如下: (1)在c:\Windows\System32\etc\hosts文件中 ...

  7. [LuoguP2158][SDOI2008]仪仗队

    [LuoguP2158][SDOI2008]仪仗队(Link) 现在你有一个\(N \times N\)的矩阵,求你站在\((1,1)\)点能看到的点的总数. 很简洁的题面. 这道题看起来很难,但是稍 ...

  8. SDN测量论文粗读(三)9.24

    Jaal: Towards Network Intrusion Detection at ISP Scale 论文来源:CoNext 发表时间:2015 解决问题及所做贡献:Jaal:大规模精细网络入 ...

  9. EF Core中外键关系的DeleteBehavior介绍(转自MSDN)

    Delete behaviors Delete behaviors are defined in the DeleteBehavior enumerator type and can be passe ...

  10. HTML和CSS基础知识

    html基本结构<html>内容</html> html开始标记<head>内容</head> html文件头标记<title>内容< ...