LeetCode 172. Factorial Trailing Zeroes

问题来源LeetCode 172. Factorial Trailing Zeroes

问题描述

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

Note: Your solution should be in logarithmic time complexity.

问题分析

虽然这道题是Easy难度,但是刚开始的时候没有想到有什么好的解法。后来想到0只能由2*5得到。所以说判断这道题除以5就可以了。
比如10/5 =2;由2*510产生了2个0,n=25。计算n/5,可以得到5个5,分别来自其中的5, 10, 15, 20, 25,但是在25中其实是包含2个5的,这一点需要注意。

所以说除了计算n/5, 还要计算n/5/5, n/5/5/5, n/5/5/5/5, …, n/5/5/5,,,/5直到商为0,然后就和,就是最后的结果。

Java代码


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

LeetCode学习笔记持续更新

GitHub地址 https://github.com/yanqinghe/leetcode

CSDN博客地址 http://blog.csdn.net/yanqinghe123/article/category/7176678

LeetCode 172. Factorial Trailing Zeroes相关推荐

  1. Leetcode 172 Factorial Trailing Zeroes

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

  2. LeetCode(172) Factorial Trailing Zeroes

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

  3. leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)

    数字的末尾为0实际上就是乘以了10,20.30.40其实本质上都是10,只不过是10的倍数.10只能通过2*5来获得,但是2的个数众多,用作判断不准确. 以20的阶乘为例子,造成末尾为0的数字其实就是 ...

  4. 172. Factorial Trailing Zeroes

    /**172. Factorial Trailing Zeroes *2016-6-4 by Mingyang* 首先别忘了什么是factorial,就是阶乘.那么很容易想到需要统计* (2,5)对的 ...

  5. leetcode python3 简单题172. Factorial Trailing Zeroes

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百七十二题 (1)题目 英文: Given an integer n, retu ...

  6. 【LeetCode】172 - Factorial Trailing Zeroes

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

  7. Leet Code OJ 172. Factorial Trailing Zeroes [Difficulty: Easy]

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

  8. 172 Factorial Trailing Zeroes(阶乘后的零)————附带详细思路和代码

    文章目录 0 效果 1 题目 2 思路 3 代码 0 效果 1 题目 Given an integer n, return the number of trailing zeroes in n!. N ...

  9. 【leetcode❤python】172. Factorial Trailing Zeroes

    #-*- coding: UTF-8 -*- #给定一个整数N,那么N的阶乘N!末尾有多少个0? 比如:N=10,N!=3628800,N!的末尾有2个0. #所有的尾部的0可以看做都是2*5得来的, ...

最新文章

  1. 一笔画 (25 分)
  2. 跟安全技术大师学习黑客攻防技术 ——《黑客攻防技术宝典:web实战篇》
  3. Flutter开发之数据存储-2-文件存储(33)
  4. 多传感器信息融合算法总结
  5. 【CV秋季划】人脸识别经典难题介绍,抗遮挡,跨年龄,异质源,少样本等
  6. 超简单的利用plist 查看ipa包名及其它信息
  7. 开发短信发送程序的几则技巧
  8. 【LeetCode笔记】322. 零钱兑换(Java、动态规划)
  9. 【Linux】【secureCRT】下载,安装,激活攻略
  10. win10系统安装postgresql后无法连接
  11. Skyfree退休公告
  12. 航空订票系统(javaweb项目)
  13. singleTask和startActivityResult
  14. 仅三行代码的按键扫描程序,绝对够经典(秒杀郭天祥讲解的按键扫描)
  15. 【Python实用工具】查询本机IP地址
  16. S7-1200/1500获取本地IP地址(不使用库)
  17. 关于typescript中的extends和infer以及用法
  18. world中英文字体分离、字符间距较大解决
  19. Unity3d 游戏特效制作实战教程 第二部
  20. 效率倍增!12 个必知必会的 Python 数据处理技巧!

热门文章

  1. 22.10.29 CF-1294C
  2. 550-c 550-s
  3. 159商业模式和代理制度详解
  4. 力扣105和106题
  5. Spring事务的实现原理
  6. 2020-11-25计算机操作系统课后习题答案
  7. 计算机休眠需要重新开机,再确定; 以上的方法能大致解决电脑休眠自动重启的问题...
  8. Axure PR9左侧页面概要元件母版不见了,在哪开启
  9. IE8浏览器不支持js的trim方法怎么办?
  10. 让仓库“零误差”,WMS仓库管理系统助力供应链升级