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:

  1. This problem is simple, but you may run into a complexty and easy-wrong way.
  2. The below is a complex solution(wrong) try to use the same idea from "count primes".
  3.  
  4. A complex and wrong solution:
  5. public class Solution {
  6. public boolean isUgly(int num) {
  7. if (num <= 0)
  8. return true;
  9. // throw new IllegalArgumentException("The passed in argument is not legal");
  10. if (num == 1)
  11. return true;
  12. boolean[] check_board = new boolean[num+1];
  13. Arrays.fill(check_board, true);
  14. for (int i = 2; i <= Math.sqrt(num); i++) {
  15. if (check_board[i] == true) {
  16. for (int j = i*2; j <= num; j = j+i) {
  17. check_board[j] = false;
  18. if (j == num) {
  19. if (!(i == 2 || i == 3 || i== 5)))
  20. return true;
  21. }
  22. }
  23. }
  24. }
  25. return false;
  26. }
  27. }
  28.  
  29. Why we so many uncessary computing and memeory reasource for a sigle number???
  30. (Something must be wrong for the solution)
  31.  
  32. If a number is a ugly number, if must consist of (2, 3 5) through following way.
  33. num = (2^i) * (3^j) * (5^k) * 1
  34.  
  35. Why not we strip out prime factor(2, 3, 5) one by one from num, then check if "num == 1"?
  36. How to strip out prime factor from a integer?
  37. Assume: a is the prime factor
  38. while (num % a == 0) {
  39. num = num / a;
  40. }
  41. Reason: since num could be fully divided by a (num % a == 0), we could still strip a from num.
  42. This idea is very tricky compared with our past experience in using array. Take care!

Solution:

  1. public class Solution {
  2. public boolean isUgly(int num) {
  3. if (num <= 0)
  4. return false;
  5. if (num == 1)
  6. return true;
  7. int[] x = {2, 3, 5};
  8. for (int a : x) {
  9. while (num % a == 0) {
  10. num = num / a;
  11. }
  12. }
  13. return num == 1;
  14. }
  15. }

[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. php 5.3起弃用session_register

    最近下了dedecms V5.7时,在登陆后台时,用户名和密码也没错,就是跳转不走,进不了后台管理页面,追踪了好久才发现根目录/include/userlogin.class.php中289行左右的位 ...

  2. favicon.ico显示,favicon显示,favicon图标显示

    favicon.ico显示,favicon显示,favicon图标显示 >>>>>>>>>>>>>>>> ...

  3. 《高性能js》读书笔记

    第一章:加载和执行 .浏览器的JavaScript的引擎是编译器层的优化: .当浏览器执行JavaScript代码时,不能同时做其他任何事情(单一进程),意味着 .主流浏览器都允许并行下载JS. .减 ...

  4. 局域网之php项目IP访问共享

    局域网之php本地项目共享 该文章主要介绍本地php项目在局域网内的共享访问,主要体现为通过本地ip地址访问项目 做法如下: 1.更改本地盘host文件(winds目录为:C:\Windows\Sys ...

  5. C++ 常见问题

    1:保证编译后方法名不被修改:  The: extern "C" { function declarations here in h file } will disable C++ ...

  6. windows 8 vpn 错误解决

    最近微软发布了Windows 8 RTM版,很多朋友也安装了,我当然也不例外.这几天就有不少朋友问我VPN连接无论怎么都说密码错误不能验证,于是,便连接VPN进行了下测试,如下: 配置好VPN,步凑不 ...

  7. 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 ...

  8. javascript判断设备类型-手机(mobile)、安卓(android)、电脑(pc)、其他(ipad/iPod/Windows)等

    使用device.js检测设备并实现不同设备展示不同网页 html代码: <!doctype html> <html> <head> <meta charse ...

  9. linux 的一些 不常见的指标

    1. linux 的理论 最大用户数   2^32 -1   数据来源  linux就是这个范 (没验证) 2. mv 竟然不能修改文件更新时间

  10. HTML+JS版本的俄罗斯方块

    <!doctype html><html><head></head><body> <div id="box" st ...