p74 阶乘末尾0的个数(leetcode 172)

一:解题思路

Time:O(log_5(n)),Space:O(1)

二:完整代码示例 (C++版和Java版)

C++:

class Solution {
public:
    int trailingZeroes(int n)
    {
        int count = 0;

        while (n > 0)
        {
            n /= 5;
            count += n;
        }

        return count;
    }
};

Java:

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

 

posted @ 2020-04-03 16:10  repinkply  阅读(128)  评论(0)    收藏  举报