[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 ...
随机推荐
- php 5.3起弃用session_register
最近下了dedecms V5.7时,在登陆后台时,用户名和密码也没错,就是跳转不走,进不了后台管理页面,追踪了好久才发现根目录/include/userlogin.class.php中289行左右的位 ...
- favicon.ico显示,favicon显示,favicon图标显示
favicon.ico显示,favicon显示,favicon图标显示 >>>>>>>>>>>>>>>> ...
- 《高性能js》读书笔记
第一章:加载和执行 .浏览器的JavaScript的引擎是编译器层的优化: .当浏览器执行JavaScript代码时,不能同时做其他任何事情(单一进程),意味着 .主流浏览器都允许并行下载JS. .减 ...
- 局域网之php项目IP访问共享
局域网之php本地项目共享 该文章主要介绍本地php项目在局域网内的共享访问,主要体现为通过本地ip地址访问项目 做法如下: 1.更改本地盘host文件(winds目录为:C:\Windows\Sys ...
- C++ 常见问题
1:保证编译后方法名不被修改: The: extern "C" { function declarations here in h file } will disable C++ ...
- windows 8 vpn 错误解决
最近微软发布了Windows 8 RTM版,很多朋友也安装了,我当然也不例外.这几天就有不少朋友问我VPN连接无论怎么都说密码错误不能验证,于是,便连接VPN进行了下测试,如下: 配置好VPN,步凑不 ...
- 283. Move Zeroes(C++)
283. Move Zeroes Given an array nums, write a function to move all 0's to the end of it while mainta ...
- javascript判断设备类型-手机(mobile)、安卓(android)、电脑(pc)、其他(ipad/iPod/Windows)等
使用device.js检测设备并实现不同设备展示不同网页 html代码: <!doctype html> <html> <head> <meta charse ...
- linux 的一些 不常见的指标
1. linux 的理论 最大用户数 2^32 -1 数据来源 linux就是这个范 (没验证) 2. mv 竟然不能修改文件更新时间
- HTML+JS版本的俄罗斯方块
<!doctype html><html><head></head><body> <div id="box" st ...