263. Ugly Number

Easy

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.

Example 1:

Input: 6
Output: true
Explanation: 6 = 2 × 3

Example 2:

Input: 8
Output: true
Explanation: 8 = 2 × 2 × 2

Example 3:

Input: 14
Output: false
Explanation: 14 is not ugly since it includes another prime factor 7.

Note:

  1. 1 is typically treated as an ugly number.
  2. Input is within the 32-bit signed integer range: [−231,  231 − 1].
package leetcode.easy;

public class UglyNumber {
public boolean isUgly(int num) {
if (num <= 0) {
return false;
}
while (num % 2 == 0) {
num = num / 2;
}
while (num % 3 == 0) {
num = num / 3;
}
while (num % 5 == 0) {
num = num / 5;
}
return num == 1;
} @org.junit.Test
public void test() {
System.out.println(isUgly(6));
System.out.println(isUgly(8));
System.out.println(isUgly(14));
System.out.println(isUgly(0));
}
}

LeetCode_263. Ugly Number的更多相关文章

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

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

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

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

  3. [LeetCode] Ugly Number 丑陋数

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

  4. 【13_263】Ugly Number

    简单题 Ugly Number My Submissions Question Total Accepted: 32635 Total Submissions: 94009 Difficulty: E ...

  5. Leetcode 313. super ugly number

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

  6. Leetcode 264. Ugly Number II

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

  7. LeetCode 263 Ugly Number

    Problem: Write a program to check whether a given number is an ugly number. Ugly numbers are positiv ...

  8. [LeetCode] Ugly Number II

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

  9. [LeetCode] Ugly Number

    Ugly Number Total Accepted: 20760 Total Submissions: 63208 Difficulty: Easy Write a program to check ...

随机推荐

  1. C语言实验1—— C中的指针和结构体

    问题 实现一个算法,检测单链表中是否有环,如果有环还要得到环的入口. 分析 判断是否有环:快慢指针法(也叫“龟兔赛跑”),慢指针每次移动一位,快指针每次移动两位,如果有环,他们一定会相遇. 求环的入口 ...

  2. 谈MongoDB的应用场景

    转载自:http://blog.csdn.net/adparking/article/details/38727911 MongoDB的应用场景在网上搜索了下,很少介绍关于传统的信息化应用中如何使用M ...

  3. python与各数据库的交互

    from redis import StrictRedis from pymongo import MongoClient import pymysql #redis客户端 redis_cli = S ...

  4. JS发送验证码;并设置cookie

    Tool.send_code = function(obj) { var isCheck = true, form = $('#editInfo_Form'); var mobile = form.f ...

  5. RS译码的描述

    在描述Reed-Solomon码的译码时,需要确定错误多项式的系数,然后进行搜索.通常,错误多项式的最高次数与错误的符号数相同. 如: λ(X)= λ0+ λ1X+ ...+ λνXν. 设该RS码最 ...

  6. vue-router 的原理

    1. hash 修改的时候:history.pushState('名字', null, '/xxx') || location.hash = '/xxx' 回退的时候:window.addEventL ...

  7. C++标准库分析总结(八)——<仿函数、适配器、istream_iterator、ostream_iterator、bind>

    一.仿函数定义 仿函数是STL中最简单的部分,存在的本质就是为STL算法部分服务的,一般不单独使用.仿函数(functors)又称为函数对象(function objects),虽然函数指针虽然也可以 ...

  8. mysql in()后子查询优化

    线上数据发现一条数据大量等待的现象,通过explain发现这个sql写法存在问题,这里简单记录一下. 业务场景是这样: 存在购物车和费用两张表,购物车数据是购买商品时生成,用于记录购买商品数据,同时购 ...

  9. 多个List 或 Array 进行 合并

    1.调用 var aaa = new List<string>() { "0" }; var a1 = new List<string>() { " ...

  10. rapidxml学习

    参考: 官网http://rapidxml.sourceforge.net/ https://blog.csdn.net/wqvbjhc/article/details/7662931 http:// ...