[LeetCode#263]Factorial Trailing Zeroes
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的更多相关文章
- LeetCode Day4——Factorial Trailing Zeroes
/* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...
- [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- 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 ...
- 【leetcode】Factorial Trailing Zeroes
题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- ✡ 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 ...
- 【leetcode】Factorial Trailing Zeroes(easy)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- 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 ...
- leetcode:Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. 最初的代码 class Solution { public: int t ...
随机推荐
- CentOS7安装Python3.5
2. 安装Python的依赖包 yum -y groupinstall "Development tools" yum -y install openssl-devel sqlit ...
- ashx+html+ajax
HTML: $(function() { ajax("NewsList.ashx", function(resText) { document.getElementById(&qu ...
- 使用原生JS编写ajax操作XMLHttpRequst对象
ajax其本质就是XMLHttpRequest,现在jquery调用异步的方法很方便,但是也不能忘记原生的JS去编写ajax; 需要注意的是,很多人在写的时候喜欢只用XMLHttpRequest对象r ...
- JS关闭窗口或JS关闭页面的几种代码
//JS定时自动关闭窗口 <script language="javascript"> <!-- function closewin(){ self.opener ...
- Python编写相关注意事项
1.# -*- coding: utf-8 -*-代码首部添加这个,不然会报Non_ASCII charater错误 python闭包:实际应用场景1.保持闭包运行完后的环境: 2.根据外部作用域的局 ...
- Stream To String , String To Stream
public static string StreamToString(Stream stream) { stream.Position = 0; using (StreamReader stremR ...
- 第一章JSP基础语法
jsp页面元素构成 jsp页面组成部分有:指令,注释,静态内容,表达式,小脚本,声明. jsp指令 page指令:通常位于jsp页面的顶端,同一个页面可以有多个page指令 include指令:将一个 ...
- 线性回顾-generalize issue
Ein的平均,Eout的平均 用这个平均来justify linear regresssion能够用的很好 noise level 资料里有多少的杂讯 等一下要证明的事情 predictions + ...
- .net中String是引用类型还是值类型 以及 C#深层拷贝浅层拷贝
http://www.cnblogs.com/yank/archive/2011/10/24/2204145.html http://www.cnblogs.com/zwq194/archive/20 ...
- 非常好用的正则表达式"\\s+" - 匹配任意空白字符
说起来,博主使用过的正则场景虽然不多,但是就是在这当中,我发现"\\s+"真好用! 详解 "\\s+" 正则表达式中\s匹配任何空白字符,包括空格.制表符.换页 ...