/*
* @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. 10分钟开始.Net Core

    .Net Core 2.0发布了,API也越来越多.此时不用.Net Core,更待何时? 安装.Net Core SDK         首先,我们当然要先装.Net Core SDK,在这里下载( ...

  2. python 脚本运行时报错: AttributeError: 'module' object has no attribute ***

    最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...

  3. yarn logs -applicationId [applicationID]

    yarn logs -applicationId application_1435648583743_0001 报错: tmp/logs/.../application_1435648583743_0 ...

  4. 本机未装Oracle数据库时Navicat for Oracle 报错:Cannot create oci environment 原因分析及解决方案

    因为要更新数据库加个表,远程桌面又无法连接...所以就远程到另外一台电脑,然后用navicat通过内网修改目标数据库. 一直用着navicat操作数据库,所以很速度的弄好然后新建连接进入数据库. 然而 ...

  5. html转canvas html2canvas.js

    $("#btn-html2canvas").on("click",function(){//btn-html2canvas为按钮 //content-main为 ...

  6. 【RabbitMQ】3、工作队列模式(work模式)

    上一篇博客的作为rabbitMQ的入门程序,也是简单队列模式,一个生产者,一个消费者,今天这篇博客介绍work模式,一个生产者,多个消费者,下面的例子模拟两个消费者的情况. 图示:         一 ...

  7. 死磕salt系列-salt配置文件

    这篇文件主要用来解释一下salt配置中常用的参数,其他的参数可以参考官网文档. 基础参数 interface: 服务器监听地址. ipv6: 是否启用ipv6. max_open_files: 最大文 ...

  8. Hadoop学习之路(二十七)MapReduce的API使用(四)

    第一题 下面是三种商品的销售数据 要求:根据以上数据,用 MapReduce 统计出如下数据: 1.每种商品的销售总金额,并降序排序 2.每种商品销售额最多的三周 第二题:MapReduce 题 现有 ...

  9. ionic和angularjs的区别?

    a.ionic是一个用来开发混合手机应用的,开源的,免费的代码库.可以优化HTML.css和js的性能,构建高效的应用程序,而且还可以用于构建sass和angularJS的优化 b.AngularJS ...

  10. js中时间的操作

    var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();    //获取完整的年份(4位,1 ...