Problem:

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.

Analysis:

This problem is simple, but you may run into a complexty and easy-wrong way.
The below is a complex solution(wrong) try to use the same idea from "count primes". A complex and wrong solution:
public class Solution {
public boolean isUgly(int num) {
if (num <= 0)
return true;
// throw new IllegalArgumentException("The passed in argument is not legal");
if (num == 1)
return true;
boolean[] check_board = new boolean[num+1];
Arrays.fill(check_board, true);
for (int i = 2; i <= Math.sqrt(num); i++) {
if (check_board[i] == true) {
for (int j = i*2; j <= num; j = j+i) {
check_board[j] = false;
if (j == num) {
if (!(i == 2 || i == 3 || i== 5)))
return true;
}
}
}
}
return false;
}
} Why we so many uncessary computing and memeory reasource for a sigle number???
(Something must be wrong for the solution) If a number is a ugly number, if must consist of (2, 3 5) through following way.
num = (2^i) * (3^j) * (5^k) * 1 Why not we strip out prime factor(2, 3, 5) one by one from num, then check if "num == 1"?
How to strip out prime factor from a integer?
Assume: a is the prime factor
while (num % a == 0) {
num = num / a;
}
Reason: since num could be fully divided by a (num % a == 0), we could still strip a from num.
This idea is very tricky compared with our past experience in using array. Take care!

Solution:

public class Solution {
public boolean isUgly(int num) {
if (num <= 0)
return false;
if (num == 1)
return true;
int[] x = {2, 3, 5};
for (int a : x) {
while (num % a == 0) {
num = num / a;
}
}
return num == 1;
}
}

[LeetCode#263]Factorial Trailing Zeroes的更多相关文章

  1. LeetCode Day4——Factorial Trailing Zeroes

    /* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...

  2. [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  3. LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  4. Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes

    题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...

  5. 【leetcode】Factorial Trailing Zeroes

    题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...

  6. ✡ leetcode 172. Factorial Trailing Zeroes 阶乘中的结尾0个数--------- java

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  7. 【leetcode】Factorial Trailing Zeroes(easy)

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  8. Java for LeetCode 172 Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  9. leetcode:Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. 最初的代码 class Solution { public: int t ...

随机推荐

  1. day-9

    /* 考前第9天 区间*的线段树居然卡住了23333 明天再搞搞 今天针对考试复习了几个板子 手动堆都打了 实测比priority快 下午考试成了炮灰233333 晚上复习矩阵乘法 手推9*9矩阵 可 ...

  2. 覆盖(override)和重载(overload)

    覆盖(override)重写和 重载(overload) 继承,重写--->多态   我懂了,你懂吗 ,不看看文章 java 子类重写父类的方法应注意的问题 Java多态性理解

  3. [功能帮助类] C# BaseRandom随机数,随机字符,可限制范围-帮助类 (转载)

    点击下载 BaseRandom.rar 主要功能如下 .产生随机字符 .产生随机数 .在一定范围内产生随机数 看下面代码吧 /// <summary> /// 编 码 人:苏飞 /// 联 ...

  4. Javascript基础学习(3)_对象和数组

    一.对象是一种无序的属性集合,每个属性都有自己的名字和值. 1.创建对象 花括号内逗号分隔 var person = { "Name" : "LiCheng", ...

  5. 数据库msqlserver的几种类型及解决MSSQLServer服务启动不了的问题

    从08年开始学习了sqlserver数据库之后,就一直以为sqlserver只有版本的区分,没有类型的差异:总以为从Sql2000. sql2005到sql2008.sql2012,微软出口的数据库, ...

  6. oracle rowid 使用

    ROWID是数据的详细地址,通过rowid,oracle可以快速的定位某行具体的数据的位置. ROWID可以分为物理rowid和逻辑rowid两种.普通的堆表中的rowid是物理rowid,索引组织表 ...

  7. (七)Struts2 验证框架

    所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:Struts2 验证简介 Struts2 基于Struts2 拦截器,为 ...

  8. 2014-11-9------- 设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  9. 练习SignalR使用

    前言 随着Ajax越来越普遍的使用,前端页面跟后台服务也越来越密切的进行交互,实现前后端进行实时的消息传递尤为重要,一文件上传为例,现在普遍使用ajax上传然后通过flash进行文件进度的显示,这是目 ...

  10. Python 学习之urllib模块---用于发送网络请求,获取数据

    1.urllib urllib是Python标准库的一部分,包含urllib.request,urllib.error,urllib.parse,urlli.robotparser四个子模块. (1) ...