LintCode 2. 尾部的零

  • LintCode 2. 尾部的零
  • 设计一个算法,计算出 n 阶乘中尾部零的个数。

样例

  • 11! = 39916800,因此应该返回 2。

Java 代码

public class Solution {
    /*
     * @param n: An integer
     * @return: An integer, denote the number of trailing zeros in n!
     */
    public long trailingZeros(long n) {
        // write your code here, try to do it without arithmetic operators.
        long sum = 0;
        while (n > 0) {
            n = n / 5;
            sum = sum + n;
        }
        return sum;
    }
}

参考资料

相关文章:

  • 2021-12-15
  • 2021-06-26
  • 2021-08-01
  • 2021-07-21
  • 2022-12-23
  • 2021-08-27
  • 2021-12-21
猜你喜欢
  • 2021-12-14
  • 2021-07-20
  • 2022-12-23
  • 2021-12-01
  • 2021-09-01
  • 2021-08-20
  • 2022-12-23
相关资源
相似解决方案