leetcode 172. Factorial Trailing Zeroes

来源:互联网 发布:阿里巴巴域名备案 编辑:程序博客网 时间:2024/06/06 02:49

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.


阶乘结果最后0的个数
   关键在于 5或者5的倍数出现的次数! 
   5!   5出现1次
   10! 出现两次, 5 和 10。
   25! 出现6次,因为 25本身是两次。5,10,15,20,25(两次)
   125! 出现 25 + 5 + 1 次。意思是125之前的数能提5出来的有25个。提了5之后还能提5的有5个。提了25之后还能提5的有一个,就是125本身。


class Solution {public:    int trailingZeroes(int n)     {        int ret = 0;        while (n)        {            ret += n / 5;            n /= 5;        }        return ret;      }};




原创粉丝点击