[LintCode] Trailing Zeroes 末尾零的个数
生活随笔
收集整理的這篇文章主要介紹了
[LintCode] Trailing Zeroes 末尾零的个数
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
Write an algorithm which computes the number of trailing zeros in n factorial.
Have you met this question in a real interview?? Yes Example11! = 39916800, so the out should be 2
Challenge?O(log N) time
?
LeetCode上的原題,請(qǐng)參見(jiàn)我之前的博客Factorial Trailing Zeroes。
?
解法一:
class Solution {public:// param n : description of n// return: description of return long long trailingZeros(long long n) {long long res = 0;while (n > 0) {res += n / 5;n /= 5;}return res;} };?
解法二:
class Solution {public:// param n : description of n// return: description of return long long trailingZeros(long long n) {return n == 0 ? 0 : n / 5 + trailingZeros(n / 5);} };?
轉(zhuǎn)載于:https://www.cnblogs.com/grandyang/p/6201067.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的[LintCode] Trailing Zeroes 末尾零的个数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: svn版本信息自动更新到源代码
- 下一篇: 基于 Node.js 平台的web开发框