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. 基于Memcache的分布式缓存系统详解

    文章不是简单的的Ctrl C与V,而是一个字一个标点符号慢慢写出来的.我认为这才是是对读者的负责,本教程由技术爱好者成笑笑(博客:http://www.chengxiaoxiao.com/)写作完成. ...

  2. Codeforces Round #310 (Div. 2)--B

    http://codeforces.com/problemset/problem/556/B 题意:给定n个数字且都小于n,然后每次循环第2k+1个数字+1,第2k个数字减一,k=0,1,2...n/ ...

  3. 文件图标css样式

    .list-list .ico-bookfolder { background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEU ...

  4. HTML基础(1) 全局架构标签,特殊字符

    最基本的网页文件组成部分 其中 <head></head> 这个标签对中内容不会显示在网页中 <body></body> 中的内容可以显示在网页中. b ...

  5. 在.Net中进行跨线程的控件操作(上篇:Control.Invoke)

    本文的重点在于介绍如何在多线程编程中,从非UI线程上访问界面中的控件.有过多线程编程经验的人都知道,当我们在非UI线程上试图给一个界面中的控件赋值的时候,比如说label的Text属性,系统会抛出一个 ...

  6. html复选框多行排列布局

    前言:写这篇文章,主要是在于总结一下自己的心得体会... 公司的产品需要实现操作权限配置,我们实现的方式是,左边是“产品”=>“模块”树,右边是由“菜单”和“按钮”复选框按钮.如下图:

  7. oracle 非空闲等待事件排查

    想必大家都知道Oracle的等待时间分为两种,一种我们称之为“空闲等待事件”,另外一种称之为“非空闲等待事件”.“空闲等待事件”——作为DBA可以不用过分的关注这类等待事件.“非空闲等待事件”——当D ...

  8. Maven3(笔记一)

    第一节:Maven 简介 百度百科:Maven 官网:http://maven.apache.org/ 第二节:Maven 安装与配置 Maven 下载:http://maven.apache.org ...

  9. jQuery 效果方法

    jQuery 效果方法 下面的表格列出了所有用于创建动画效果的 jQuery 方法. 方法 描述 animate() 对被选元素应用"自定义"的动画 clearQueue() 对被 ...

  10. Jmeter软件测试3--发送二进制报文

    一直用Jmeter测试post接口,但报文信息都是明文方式,今天测试兄弟求助二进制报文如何使用Jmeter测试,查看了项目源码,报文中不仅采用二进制,而且还用java.util.zip进行了压缩,从晚 ...