首页 > 学院 > 开发设计 > 正文

172. Factorial Trailing Zeroes

2019-11-08 02:37:36
字体:
来源:转载
供稿:网友

题目

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

Note: Your solution should be in logarithmic time complexity.

Credits: Special thanks to @ts for adding this PRoblem and creating all test cases.

Subscribe to see which companies asked this question.


思路

计算n的阶乘的最后有几个零,就是计算因数中2和5的最小个数,5的个数肯定比2的少,就是计算因数中5的个数


代码

class Solution {public: int trailingZeroes(int n) { //计算n的阶乘的最后有几个零,就是计算因数中2和5的最小个数,5的个数肯定比2的少,就是计算因数中5的个数 if(n <= 4) { return 0; } int times = 0; while(n) { times += n/5; n /= 5; } return times; }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表