172. Factorial Trailing Zeroes

JJ·2020년 12월 19일
0

Algorithms

목록 보기
16/114
class Solution {
    public int trailingZeroes(int n) {
        int two = 0;
        int five = 0;
        int twon = n;
        int fiven = n;
        while (twon / 2 >= 1) {
            two = two + twon / 2;
            twon = twon/2;
        }
        
        while (fiven / 5 >= 1) {
            five = five + fiven / 5;
            fiven = fiven / 5;
        }
        
        return (two > five ? five : two);
    }
}

Runtime: 0 ms, faster than 100.00% of Java online submissions for Factorial Trailing Zeroes.

Memory Usage: 35.8 MB, less than 76.82% of Java online submissions for Factorial Trailing Zeroes.

머리좀 썼다^^

근데 생각해보니깐 팩토리알이니.. 2는 셀필요 x
2보다 부조건 5가 적다는 점~

class Solution {
    public int trailingZeroes(int n) {
        int five = 0;
        int fiven = n;
        
        while (fiven / 5 >= 1) {
            five = five + fiven / 5;
            fiven = fiven / 5;
        }
        
        return five;
    }
}

Runtime: 0 ms, faster than 100.00% of Java online submissions for Factorial Trailing Zeroes.

Memory Usage: 35.7 MB, less than 76.82% of Java online submissions for Factorial Trailing Zeroes.

0개의 댓글