关于[LeetCode]Factorial Trailing Zeroes O(logn)解法的理解
题目描述:
Given an integer n, return the number of trailing zeroes in n!.
题目大意:
给定一个整数n,返回n!(n的阶乘)结果中后缀0的个数(如5!=120,则后缀中0的个数为1)。
解题思路:
int trailingZeroes(int n) {
return (n/>)?trailingZeroes(n/)+n/:;
}
首先这是LeetCode中时间复杂度为O(logn)的解法。
可以简单的知道,阶乘结果中后缀0的个数取决于n!中因数5的个数,因为5x2等于10,这样就出现了0,而因数中2的个数总是比5的个数多的,如5!=1x2x3x4x5,其中5x2得一个0,因数5的个数只有1个,因数2的个数由3个(2,4=2x2)。
重点是为什么上述代码可以求出阶乘中因数5的个数?
让我们举个阶乘61!的例子,一开始61/5=12,这说明1到61中有12个数可以被5整除(即具有因数5),分别是
5
而其他数相乘不会产生0,所以不用再考虑其他数了。
可以看出这12个数中都包含了因数5,但并不是每个数中都只包含1个因数5,如25=5x5,它包含两个因数5。所以,为了计算所有的因数5的个数,我们可以把这12个数做些改变,如
5 =5x1 =5x2
=5x3 =5x4
=5x5 =5x6
=5x7 =5x8
=5x9 =5x10
=5x11 =5x12
可以看出,我们从12个数中找到了12个因数5,剩下了1到12的序列。1到12的序列中也是有因数5的,那么1到12的序列中因数5的个数不就相当于找12!阶乘结果因数5的个数(即阶乘结果中后缀0的个数),这时候就重复递归,即代码中的trailingZeroes(n/5);
当n/5小于0,n小于5,自然就没有因数5了。
以上就是对LeetCode中时间复杂度为O(logn)的解法的理解,本文为本作者原创,转载请注明出处!
关于[LeetCode]Factorial Trailing Zeroes O(logn)解法的理解的更多相关文章
- LeetCode Factorial Trailing Zeroes Python
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...
- [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- LeetCode Factorial Trailing Zeroes
原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...
- [LeetCode] Factorial Trailing Zeroes 阶乘末尾0
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- Python3解leetcode Factorial Trailing Zeroes
问题描述: Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 ...
- LeetCode Factorial Trailing Zeroes (阶乘后缀零)
题意:如标题 思路:其他文章已经写过,参考其他. class Solution { public: int trailingZeroes(int n) { <? n/: n/+trailingZ ...
- LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)
172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...
- 【LeetCode】172. Factorial Trailing Zeroes
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...
- LeetCode Day4——Factorial Trailing Zeroes
/* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...
随机推荐
- 暑假练习赛 007 B - Weird Cryptography
Weird Cryptography Description standard input/outputStatements Khaled was sitting in the garden unde ...
- Pseudoforest(伪最大生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- Log4j – Log4j 2 API
Overview The Log4j 2 API provides the interface that applications should code to and provides the ad ...
- zepto在操作dom的selected和checked属性时尽量使用prop方法
zepto在操作dom的selected和checked属性时尽量使用prop方法.
- JAVAscript学习笔记 js事件 第一节 (原创) 参考js使用表
<!DOCTYPE html> <html lang="en" onUnload="ud()"> <head> <me ...
- Java_String_01_由转义字符串得到其原本字符串
在开发企业微信电子发票之拉取电子发票接口的时候,微信服务器会发送给我们一个2层的转义字符串,而我们要想得到我们想要的结果,就需要进行一些处理: 反转义+去除首尾双引号. 一.需求 现有一个字符串 st ...
- C++分布式实时应用框架 (Cpp Distributed Real-time Application Framework)----(一):整体介绍
C++分布式实时应用框架 (Cpp Distributed Real-time Application Framework) 在现今软件系统纷纷"云化"的浪潮下,各种支持" ...
- HBase流量限制和表负载均衡剖析
1.概述 在HBase-1.1.0之前,HBase集群中资源都是全量的.用户.表这些都是没有限制的,看似完美实则隐患较大.今天,笔者就给大家剖析一下HBase的流量限制和表的负载均衡. 2.内容 也许 ...
- CopyOnWriteArrayList并发容器
CopyOnWriteArrayList并发容器 Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容,当某个人想要修改这个内容的时候,才 ...
- Asp.Net 为什么需要异步
之前看过别人提出为什么在本是多线程的Asp.Net下需要异步环境的时候,提出在Asp.Net环境下本身就是多线程,每个请求就是由一个专门IIS线程负责(咱不说Core下无IIS的情况).所以以此推论A ...