[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;
    }
}
时间: 2024-08-08 05:38:52

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

[LeetCode]172.Factorial Trailing Zeroes

题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 分析 朴素解法: 首先求出n!,然后计算末尾0的个数.(重复÷10,直到余数非0) 该解法在输入的数字稍大时就会导致阶乘得数溢出,不足取. O(logn)解法: 考虑n!的质数因子. 后缀0总是由质因子2和质因子5相乘得来的.如果我们可以计数

LeetCode Day4——Factorial Trailing Zeroes

1 /* 2 * Problem 172: Factorial Trailing Zeroes 3 * Given an integer n, return the number of trailing zeroes in n!. 4 * Note: Your solution should be in logarithmic time complexity. 5 */ 6 7 /* 8 * Solution 1 9 * 对于每一个数字,累计计算因子10.5.2数字出现的个数,结果等于10出现的

【LeetCode】Factorial Trailing Zeroes (2 solutions)

Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. Credits:Special thanks to @ts for adding this problem and creating all test cases. 对n!做质因数分解n!=2x*

Java [Leetcode 172]Factorial Trailing Zeroes

题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 解题思路: 对于阶乘而言,也就是1*2*3*...*n[n/k]代表1~n中能被k整除的个数那么很显然[n/2] > [n/5] (左边是逢2增1,右边是逢5增1)[n/2^2] > [n/5^2](左边是逢4增1,右边是逢25增1)

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 logarithmic time complexity. 解题思路: 计算n能达到的5的最大次幂,算出在这种情况下能提供的5的个数,然后减去之后递归即可,JAVA实现如下: static public int trailingZeroes(int n) { if(n<25) return n/5; lon

【LeetCode】Factorial Trailing Zeroes

n久不做题了 ,之前因为考研,然后又是假期,一直懒得做,今天开始吧 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 开始没有看到是阶乘,之后又研究复杂度的问题 代码如下: class Solution { public:     int trailingZeroes(int n) {      

LeetCode 172 Factorial Trailing Zeroes(阶乘后的零)(*)

翻译 给定一个整型n,返回n!后面的零的个数. 注意:你的解决方案应该在log时间复杂度内. 原文 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 分析 起初我看题目的时候没太注意,还以为就是求n这个数后面的零而已,虽然心想不会这么简单吧--就写了一份代码提交了,结果WA提示我5的话应该返回1,

leetcode——172 Factorial Trailing Zeroes(N!尾巴上有多少个0,算法复杂度为lg)

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity.(您的解决方案应该在对数时间复杂度.) Hide Tags: Math 题目要求:给定N,求N!的末尾有多少0.要求算法复杂度为lg 解题思路: 思路一: 想的比较简单,先实用for循环进行阶乘运算,然后mod10计算0的个数,但是在OJ检查时,超时

[C++]LeetCode: 88 Factorial Trailing Zeroes (阶乘后导零)

题目: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 思路: 我们要计算 N! 中有多少个后导0. 我们来找一下规律,考虑n!的质数因子.后缀0,只有可能是质因子2 * 质因子5得到.如果我们可以计算得到min{num(2), num(5)},就可以知道后导0的个数. 举例子: n = 5!