Write an algorithm which computes the number of trailing zeros in n factorial.

Have you met this question in a real interview?

Yes
Example

11! = 39916800, so the out should be 2

Challenge

O(log N) time

LeetCode上的原题,请参见我之前的博客Factorial Trailing Zeroes

解法一:

class Solution {
public:
// param n : description of n
// return: description of return
long long trailingZeros(long long n) {
long long res = ;
while (n > ) {
res += n / ;
n /= ;
}
return res;
}
};

解法二:

class Solution {
public:
// param n : description of n
// return: description of return
long long trailingZeros(long long n) {
return n == ? : n / + trailingZeros(n / );
}
};

[LintCode] Trailing Zeroes 末尾零的个数的更多相关文章

  1. 一步一步写算法(之n!中末尾零的个数统计)

    原文:一步一步写算法(之n!中末尾零的个数统计) [ 声明:版权所有,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 在很多面试的题目中,求n!结果中零的个数也是 ...

  2. [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  3. [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  4. [CareerCup] 17.3 Factorial Trailing Zeros 求阶乘末尾零的个数

    LeetCode上的原题,讲解请参见我之前的博客Factorial Trailing Zeroes. 解法一: int trailing_zeros(int n) { ; while (n) { re ...

  5. [LintCode] Move Zeroes 移动零

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  6. LightOj1028 - Trailing Zeroes (I)---求因子个数

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1028 题意:给你一个数 n (1<=n<=10^12), 然后我们可以把它 ...

  7. Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes

    题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...

  8. Light oj 1138 - Trailing Zeroes (III) 【二分查找 &amp;&amp; N!中末尾连续0的个数】

    1138 - Trailing Zeroes (III) problem=1138"> problem=1138&language=english&type=pdf&q ...

  9. LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

随机推荐

  1. selenium web driver 实现截图功能

    在验证某些关键步骤时,需要截个图来记录一下当时的情况 Webdriver截图时,需要引入 import java.io.File; import java.io.IOException; import ...

  2. web前端页面性能优化

    影响用户访问的最大部分是前端的页面.网站的划分一般为二:前端和后台.我们可以理解成后台是用来实现网站的功能的,比如:实现用户注册,用户能够为文章发表评论等等.而前端呢?其实应该是属于功能的表现. 而我 ...

  3. 汗,Google又调整了编译工具(升级SDK先备份!!!)

    1./tools 下的apkbuilder消失了 方法一.用老版本ADT中的apkbuilder(apkbuilder.bat--windows) 方法二.重新生成build.xml文件 2.aapt ...

  4. 利用密钥通过ssh互访

    dir(192.168.131.132):作为client rs1(192.168.131.110)和 rs2(192.168.131.131):作为server端     1.dir 运行命令:   ...

  5. SQL批量增加修改数据

    insert into A表(字段1,字段2) select 字段1,字段2 From B表 [注:字段类型.字段数应相同] --批量进行修改ID值 declare @i int begin )) F ...

  6. Java常用的技术网站

    学习Java,我会去的网站: 1.开源项目网站:https://github.com/和http://www.codeproject.com/,可以在这里搜索到别人上传的一些代码和项目 2.咨询问题的 ...

  7. CSS垂直居中

    一.单行文本垂直居中: 设置其文本的行高line-height与其父容器高度相等即可.如 <style> .test{ background-color: grey; line-heigh ...

  8. express框架

    一.express的使用 安装express 在根目录下输入 npm install express 引入express的包 const express = require("express ...

  9. js简单的设置快捷键,hotkeys捕获键盘键和组合键的输入

    设置快捷键 这是一个强健的 Javascript 库用于捕获键盘输入和输入的组合键,它没有依赖,压缩只有只有(~3kb). hotkeys on Githubhotkeys预览 创建 您将需要在您的系 ...

  10. 一个从数据库中把数据导成txt的笨办法

    create directory DIR_DUMP as '/oradata/data_dump'; CREATE OR REPLACE PROCEDURE anlp_to_txt IS testji ...