【leetcode】Factorial Trailing Zeroes(easy)

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

Note: Your solution should be in logarithmic time complexity.

思路:编程之美里有,就是找因子5的个数。

int trailingZeroes(int n) {
        int ans = 0;
        while(n > 0)
        {
            ans += n / 5;
            n /= 5;
        }
        return ans;
    }
时间: 2024-08-29 23:11:43

【leetcode】Factorial Trailing Zeroes(easy)的相关文章

【LeetCode】Factorial Trailing Zeroes (2 solutions)

Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. Credits:Special thanks to @ts for adding this problem and creating all test cases. 对n!做质因数分解n!=2x*

【LeetCode】Factorial Trailing Zeroes

n久不做题了 ,之前因为考研,然后又是假期,一直懒得做,今天开始吧 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 开始没有看到是阶乘,之后又研究复杂度的问题 代码如下: class Solution { public:     int trailingZeroes(int n) {      

【leetcode】Set Matrix Zeroes(middle)

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用额外空间,就用矩阵的第一行和第一列来标记这一行或这一列是否需要置0. 用两个bool量记录第一行和第一列是否需要置0 大神的代码和我的代码都是这个思路,但是我在画0的时候是行列分开处理的,大神的代码是一起处理的 void setZeroes(vector<vector<int> > &

【leetcode】Count and Say (easy)

The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211. Given an

【leetcode】Reverse Linked List(easy)

Reverse a singly linked list. 思路:没啥好说的.秒... ListNode* reverseList(ListNode* head) { ListNode * rList = NULL, * tmp = NULL; while(head != NULL) { tmp = rList; rList = head; head = head->next; rList->next = tmp; } return rList; }

【LeetCode】数组--合并区间(56)

写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组(Array)是一种线性表数据结构.它用一组连续的内存空间,来存储一组具有相同类型的数据.在每一种编程语言中,基本都会有数组这种数据类型.不过,它不仅仅是一种编程语言中的数据类型,还是一种最基础的数据结构. 贪心算法回顾: [LeetCode]贪心算法--买卖股票的最佳时机II(122) [LeetC

leetcode——172 Factorial Trailing Zeroes(N!尾巴上有多少个0,算法复杂度为lg)

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity.(您的解决方案应该在对数时间复杂度.) Hide Tags: Math 题目要求:给定N,求N!的末尾有多少0.要求算法复杂度为lg 解题思路: 思路一: 想的比较简单,先实用for循环进行阶乘运算,然后mod10计算0的个数,但是在OJ检查时,超时

[C++]LeetCode: 88 Factorial Trailing Zeroes (阶乘后导零)

题目: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 思路: 我们要计算 N! 中有多少个后导0. 我们来找一下规律,考虑n!的质数因子.后缀0,只有可能是质因子2 * 质因子5得到.如果我们可以计算得到min{num(2), num(5)},就可以知道后导0的个数. 举例子: n = 5!

LeetCode 172 Factorial Trailing Zeroes(阶乘后的零)(*)

翻译 给定一个整型n,返回n!后面的零的个数. 注意:你的解决方案应该在log时间复杂度内. 原文 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 分析 起初我看题目的时候没太注意,还以为就是求n这个数后面的零而已,虽然心想不会这么简单吧--就写了一份代码提交了,结果WA提示我5的话应该返回1,