【LeetCode】172. Factorial Trailing Zeroes

来源:互联网 发布:看图软件app 编辑:程序博客网 时间:2024/05/27 20:51

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.

Method 2 is original by myself. It is very close to Method 1, i need think more deeply.
Method 1 is taken from another programmer.

Method 1:

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

Method 2:

public class Solution {    public int trailingZeroes(int n) {        int count = 0;        int index = 5;        int flag = 5;        while(n >= flag){            count = count + (n / flag);            flag = flag * index;        }        return count;    }}
0 0
原创粉丝点击