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访问管理页面出现:403 Access Denied 解决方法

    点击红色框框出现以下403错误 打开context.xml文件 vim /usr/local/tomcat/webapps/manager/META-INF/context.xml <Conte ...

  2. Java并发—java.util.concurrent并发包概括(转载)

    一.描述线程的类:Runable和Thread都属于java.lang包 二.内置锁synchronized属于jvm关键字,内置条件队列操作接口Object.wait()/notify()/noti ...

  3. MySQL中的datetime与timestamp比较(转载)

    原文地址:http://database.51cto.com/art/200905/124240.htm 相同 显示 TIMESTAMP列的显示格式与DATETIME列相同.换句话说,显示宽度固定在1 ...

  4. App doesn't auto-start an app when booting the device in Android

    From Android 3.1, newly installed apps are always put into a "stopped" state and the only ...

  5. Python高阶函数-闭包

    高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回. 在这里我们首先回忆一下python代码运行的时候遇到函数是怎么做的. 从python解释器开始执行之后,就在内存中开辟了一个空间 每当 ...

  6. java注解1

    http://computerdragon.blog.51cto.com/6235984/1210969 http://blog.csdn.net/it_man/article/details/440 ...

  7. Kotlin学习记录2

    参考我的博客:http://www.isedwardtang.com/2017/09/03/kotlin-primer-2/

  8. Leaflet API 翻译(二)

    摘自:http://www.ithao123.cn/content-824673.html L.Point 显示以像素为单位的点的x,y坐标. 所以接受点对象的leaflet方法和选项都也接受他们简单 ...

  9. Linux网络性能评估工具iperf 、CHARIOT测试网络吞吐量

    网络性能评估主要是监测网络带宽的使用率,将网络带宽利用最大化是保证网络性能的基础,但是由于网络设计不合理.网络存在安全漏洞等原因,都会导致网络带宽利用率不高.要找到网络带宽利用率不高的原因,就需要对网 ...

  10. MySQL5.7导入数据报错ERROR 1067 (42000) at line 1015: Invalid default value for 'service_time'

    解决办法: 修改my.cnf,[mysqld] 下面添加sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_U ...