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. django 通过orm操作数据库

    Django Model 每一个Django Model都继承自django.db.models.Model 在Model当中每一个属性attribute都代表一个database field 通过D ...

  2. corethink功能模块探索开发(十五)后台新增按钮

    效果图: 1.首先添加数据列表上的新增按钮,让按钮显示出来 ->addTopButton('addnew') 2.让这个按钮行动起来,实现add方法 public function add(){ ...

  3. unix 全缓冲、行缓冲、无缓冲

    基于流的操作最终会调用read或者write函数进行I/O操作.为了使程序的运行效率最高,流对象通常会提供缓冲区,以减少调用系统I/O库函数的次数. 基于流的I/O提供以下3种缓冲: 全 缓冲:直到缓 ...

  4. 如何在浏览器网页中显示word文件内容

    如何在浏览器网页中显示word文件内容 把word文件读到byte[]中,再Response.OutputStream.Write(bytes)到客户端去 Page_Load事件中写: //FileS ...

  5. DATEDIFF() 返回2个日期之间的间隔

    SELECT DATEDIFF(day,'2008-12-29','2008-12-30') AS DiffDate 解释: 第一个参数 day 表示 返回2个日期间隔的 个间隔类型 是 日期:结果 ...

  6. Django 中间件简介

    Django 中间件简介 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. ...

  7. Apache 配置参数

    参数说明 1.Global Environment 全局环境配置,决定Apache服务器的全局参数3.Virtual Hosts—虚拟主机,虚拟主机不能与Main Server主服务器共存,当启用了虚 ...

  8. jdbc封装代码

    jdbc封装代码 package jdbcUtil; import java.sql.Connection; import java.sql.DriverManager; import java.sq ...

  9. jQuery图片放大预览

    在线演示 本地下载

  10. offset,client,scroll,style,getBoundingClientRect相关笔记

    1.offsetTop 功能:获取元素上外缘与最近的定位父元素内壁的距离,如果没有定位父元素,则是与文档上内壁的距离 使用方法:js document.querySelector(...).offse ...