Leetcode_172_Factorial Trailing Zeroes
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42417535
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
思路:
(1)题意为求解一个整数经过阶乘计算得到结果中有多少个0。
(2)我们知道0的个数和2和5有关,而2的个数要远远多于5的个数,所以求得5的个数即为0的个数。
(3)但是对于25、125这样由若干个5相乘组合的,需要计算待求解整数中有多少个这样的数。
(4)例如对于1000来说,1000/5=20,100/25=40,1000/125=8,1000/625=1,1000/1250=0,即0的个数为20+40+8+1=69个。
(5)希望本文对你有所帮助。
算法代码实现如下所示:
public static int trailingZeroes(int n) {
int count=0;
while(n>0){
count = count + n/5;
n=n/5;
}
return count;
}
Leetcode_172_Factorial Trailing Zeroes的更多相关文章
- 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 ...
- [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- [LintCode] Trailing Zeroes 末尾零的个数
Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...
- 【leetcode】Factorial Trailing Zeroes
题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- ✡ leetcode 172. Factorial Trailing Zeroes 阶乘中的结尾0个数--------- java
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- 【leetcode】Factorial Trailing Zeroes(easy)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- Java for LeetCode 172 Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- leetcode:Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. 最初的代码 class Solution { public: int t ...
- LeetCode Factorial Trailing Zeroes
原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...
随机推荐
- Android自定义View(LimitScrollerView-仿天猫广告栏上下滚动效果)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/53303872 本文出自:[openXu的博客] 1分析 2定义组合控件布局 3继承最外层控件 ...
- android混淆那些坑
ProGuard简介 在最新的Android Studio 2.2.2版本创建的Android工程中,module中的build.gradle有如下一段配置.这里的minifyEnabled即用来控制 ...
- 23 服务的绑定启动Demo3
MainActivity.java package com.example.day23_service_demo3; import com.example.day23_service_demo3.My ...
- Activity平移动画
Activity平移动画 效果图 添加动画文件 在res下添加anim文件夹,在anim下添加几个动画文件,分别是进入和退出的动画时间和移动距离,属性很简单,一看就懂,不磨叽了. tran_next_ ...
- 使用Contacts Contract Content Provider操作通讯录最佳实践
Android向所有被赋予READ_CONTACTS权限的应用程序提供了联系人信息数据库的完全访问权限.Contacts Contract使用3层数据模型去存储数据,下面介绍Contacts Cont ...
- SceneKit:简单的3D游戏场景搭建
SceneKit是Apple用来开发休闲3D游戏的框架,不同于底层的OpenGL库,你仅仅需要很少的代码就可以快速看到实际的3D场景效果.下面简单的聊聊搭建一个3D游戏场景需要做的事情. 首先你必须用 ...
- Java之String类型详解
字符串的特点 A:字符串一旦被赋值,就不能改变. 注意:这里指的是字符串的内容不能改变,而不是引用不能改变. B:字面值作为字符串对象和通过构造方法创建对象的不同 String s = new Str ...
- TCP连接建立系列 — 客户端接收SYNACK和发送ACK
主要内容:客户端接收SYNACK.发送ACK,完成连接的建立. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 接收入口 tcp_v4_rcv |--&g ...
- [struts2学习笔记] 第二节 使用Maven搞定管理和构造Struts 2 Web应用程序的七个步骤
本文地址:http://blog.csdn.net/sushengmiyan/article/details/40303897 官方文档:http://struts.apache.org/releas ...
- Activity简单几步支持向右滑动返回
向右滑动返回,对于屏幕过大的手机来说,在单手操作时,是一个不错的用户体验,用户不必再费力的或者用另一个手去点击屏幕左上角的返回按钮或者,手机右下角的返回按钮,轻轻向右滑动屏幕即可返回上一页,这个功能如 ...