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.

丑数的计算,分别去除数字中的2,3,5部分,如果数字被整除了那么就是丑数,否则不是,下见代码:

 class Solution {
public:
bool isUgly(int num) {
if(num == )
return false;
while(num % == ) num /= ;//exclude 2 factor
while(num % == ) num /= ;//exclude 3 factor
while(num % == ) num /= ;//exclude 5 factor
return (num == );//is Ugly
}
};

java版本如下所示,方法相同:

public class Solution {
public boolean isUgly(int num) {
if(num == ) return false;
while(num% == ) num/=;
while(num% == ) num/=;
while(num% == ) num/=;
return num == ;
}
}

LeetCode OJ:Ugly Number(丑数)的更多相关文章

  1. Ugly number丑数2,超级丑数

    [抄题]: [思维问题]: [一句话思路]:Long.valueOf(2)转换为long型再做 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图 ...

  2. [LeetCode] 263. Ugly Number 丑陋数

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  3. 263 Ugly Number 丑数

    编写程序判断给定的数是否为丑数.丑数就是只包含质因子 2, 3, 5 的正整数.例如, 6, 8 是丑数,而 14 不是,因为它包含了另外一个质因子 7.注意:    1 也可以被当做丑数.    输 ...

  4. [LeetCode] 264. Ugly Number II 丑陋数 II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  5. [LeetCode] Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  6. 【python】Leetcode每日一题-丑数2

    [python]Leetcode每日一题-丑数2 [题目描述] 给你一个整数 n ,请你找出并返回第 n 个 丑数 . 丑数 就是只包含质因数 2.3 和/或 5 的正整数. 示例1: 输入:n = ...

  7. 【python】Leetcode每日一题-丑数

    [python]Leetcode每日一题-丑数 [题目描述] 给你一个整数 n ,请你判断 n 是否为 丑数 .如果是,返回 true :否则,返回 false . 丑数 就是只包含质因数 2.3 和 ...

  8. [LeetCode] Ugly Number 丑陋数

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  9. [LeetCode] 264. Ugly Number II 丑陋数之二

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  10. Java [Leetcode 263]Ugly Number

    题目描述: Write a program to check whether a given number is an ugly number. Ugly numbers are positive n ...

随机推荐

  1. Tomcat 自定义默认网站目录

    上面访问的网址为http://192.168.0.108:8080/memtest/meminfo.jsp 需求: 现在我想访问格式为http://192.168.0.108:8080/meminfo ...

  2. ansible判定文件或者文件夹是否存在

    ansible 的常用模块中没有判定当文件存在或者不存在时,执行某个执行 使用下面方法能简单判定某个文件是否存在 --- - name: judge a file or dir is exits sh ...

  3. selector模块

    selector selectors模块,此模块允许高级和高效的I / O多路复用,构建在select模块原语上.鼓励用户使用此模块,除非他们需要精确控制所使用的操作系统级原语.( 默认使用epoll ...

  4. Python(名称空间、函数嵌套、函数对象)

    一.名称空间: 名称空间 定义:存放名字和值的绑定关系       内置名称空间 python自带的名字,如print.int.str 解释器启动就会生效   全局名称空间 文件级别定义的名字,都会放 ...

  5. php采集

    采集思路   采集程序的思路很简单大体可以分为以下几个步骤: 1. 获取远程文件源代码(file_get_contents或用fopen).    2.分析代码得到自己想要的内容(这里用正则匹配,一般 ...

  6. CNN学习笔记:卷积神经网络

    CNN学习笔记:卷积神经网络 卷积神经网络 基本结构 卷积神经网络是一种层次模型,其输入是原始数据,如RGB图像.音频等.卷积神经网络通过卷积(convolution)操作.汇合(pooling)操作 ...

  7. Python:笔记(4)——高级特性

    Python:笔记(4)——高级特性 切片 取一个list或tuple的部分元素是非常常见的操作.Python提供了切片操作符,来完成部分元素的选取 除了上例简单的下标范围取元素外,Python还支持 ...

  8. Linux系统启动管理 系统启动流程

    概述 linux启动时我们会看到许多启动信息,其过程可以分为5个阶段: BIOS自检 读取MBR 通过Boot Loader引导系统加载 加载initramfe虚拟文件系统 加载内核 运行system ...

  9. 【Head First Servlets and JSP】笔记22:直接从请求到JSP & 获取Person的嵌套属性

    直接从请求到JSP,不经过servlet <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  10. ubuntu16.04 虚拟机 安装win7/win10

    http://www.xitongcheng.com/jiaocheng/xtazjc_article_26588.html https://blog.csdn.net/sunyao_123/arti ...