172. Factorial Trailing Zeroes

1239 ワード

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が必要
副条件5は2より少ない~
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.