鱼C论坛

 找回密码
 立即注册
查看: 2439|回复: 0

[学习笔记] leetcode 172. Factorial Trailing Zeroes

[复制链接]
发表于 2019-9-16 07:59:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

  2. Example 1:

  3. Input: 3
  4. Output: 0
  5. Explanation: 3! = 6, no trailing zero.
  6. Example 2:

  7. Input: 5
  8. Output: 1
  9. Explanation: 5! = 120, one trailing zero.
  10. Note: Your solution should be in logarithmic time complexity.
复制代码


解题思路:1个5对应1个0,25对应2个0,125对应3个0,依次类推....

  1. class Solution {
  2.     public int trailingZeroes(int n) {
  3.         int res = n/5;
  4.         
  5.         while(n / 5 > 0){
  6.             
  7.             n = n/ 5;
  8.             res = res + n/5;
  9.         }
  10.         
  11.         return res;
  12.     }
  13.    
  14. }
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-13 14:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表