Skip to content

Latest commit

 

History

History
53 lines (42 loc) · 1.6 KB

172.md

File metadata and controls

53 lines (42 loc) · 1.6 KB

✏️Leetcode之PHP版题目解析(172. Factorial Trailing Zeroes)

2019-03-26 吴亲库里 库里的深夜食堂


✏️描述

给你一个整数数组,找出多数元素,多数元素表示出现的次数超过数组的二分之一。


✏️题目实例

✏️题目分析

求一个数阶乘末尾0的个数,尾数要是等于0的话,那么必定是可以被5整除的数,如果当前是!5,就有1个被5整数的数,!10就有两个,依次类推.所以这道题也就是在求一个阶乘数里面有多少个因式分子5.

       /**
           * @param Integer $n
           * @return Integer
           */
          function trailingZeroes($n) {
              $res=0;
              while($n){
                  $res +=intval($n/5);
                  $n /=5;  //分解因式分子后5的个数
              }
              return $res;
          }

✏️解法二

这道题稍微改进一下,用递归实现可以丧心病狂的把代码压缩到一行.

  /**
      * @param Integer $n
      * @return Integer
      */
     function trailingZeroes($n) {
         return   $n==0?0:intval($n/5)+$this->trailingZeroes($n/5);
     }

联系