首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【每天一道编程系列-2018.3.14】—— Trailing Zeros

【每天一道编程系列-2018.3.14】—— Trailing Zeros

作者头像
yesr
发布于 2019-03-14 04:50:26
发布于 2019-03-14 04:50:26
36400
代码可运行
举报
文章被收录于专栏:leetcode_solutionsleetcode_solutions
运行总次数:0
代码可运行

【题目描述】

 O(log N) time

Write an algorithm which computes the number of trailing zeros in n factorial.

Have you met this question in a real interview?

Yes

Example

11! = 39916800, so the out should be 2

【题目大意】

设计一个算法,计算出n阶乘中尾部零的个数

【本题答案】

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package blog;

/**
 * @author yesr
 * @create 2018-03-14 下午10:44
 * @desc
 **/
public class Test0314 {
    /*
     * param n: As desciption return: An integer, denote the number of trailing
     * zeros in n!
     */
    public long trailingZeros(long n) {
        // write your code here
        long count = 0;
        long temp=n/5;
        while (temp!=0) {
            count+=temp;
            temp/=5;
        }
        return count;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年03月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
LintCode-2.尾部的零
所有乘数因子中,2*5出现一个0,2管够,所以只需要统计因子中有多少5。 每五个数出现一个5,每五个(五个数)多出现一个5…… 对于n!,有n/5个一个五,n/5/5个多出现一个5……
悠扬前奏
2019/05/28
4110
LintCode 尾部的零题目分析代码
例子:(1000的阶乘末尾0的个数)**** 1000 / 5 + 1000 / 25 + 1000 / 125 + 1000 / 625 = 200 + 40 + 8 + 1 = 249(个**)
desperate633
2018/08/22
3330
【每天一道编程系列-2018.3.7】(Ans)
  Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.    For example, given array S = {-1 2 1 -4}, and target = 1.    The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). 
yesr
2019/03/14
3130
【每天一道编程系列-2018.2.17】(Ans)
Write a program that outputs the number of an integer input in the opposite order.
yesr
2019/03/14
4060
【每天一道编程系列-2018.2.26】(Ans)
Determine whether an integer is a palindrome. Do this without extra space. 
yesr
2019/03/14
2930
【每天一道编程系列-2018.2.21】(Ans)
  Reverse digits of an integer.    Example1: x = 123, return 321    Example2: x = -123, return -321 
yesr
2019/03/14
2920
【每天一道编程系列-2018.2.20】(Ans)
  The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)   P A H N    APLSIIG    Y I R    And then read line by line: “PAHNAPLSIIGYIR”    Write the code that will take a string and make this conversion given a number of rows:    string convert(string text, int nRows);    convert(“PAYPALISHIRING”,3) should return “PAHNAPLSIIGYIR”. 
yesr
2019/03/14
3130
【每天一道编程系列-2018.3.19】—— Digit Counts
Count the number of k's between 0 and n. k can be 0 - 9.
yesr
2019/03/14
3020
【每天一道编程系列-2018.2.11】(Ans)[补]
The number of daffodils refers to an n-digit number (n≥3), the sum of the n-th power of the digits in each of its digits, etc. On its own. (For example: 1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153). Program to find out all three daffodils.
yesr
2019/03/14
2880
【每天一道编程系列-2018.2.22】(Ans)
  Implement atoi to convert a string to an integer.    Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.    Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front. 
yesr
2019/03/14
3400
【每天一道编程系列-2018.3.5】(Ans)
Write a function to find the longest common prefix string amongst an array of strings. 
yesr
2019/03/14
2810
【每天一道编程系列-2018.3.2】(Ans)
  Given a roman numeral, convert it to an integer.    Input is guaranteed to be within the range from 1 to 3999. 
yesr
2019/03/14
3020
【每天一道编程系列-2018.2.28】(Ans)
  Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.  Note: You may not slant the container. 
yesr
2019/03/14
3320
【每天一道编程系列-2018.2.27】(Ans)
  Implement regular expression matching with support for ‘.’ and ‘*’. ‘.’ Matches any single character. ‘*’ Matches zero or more of the preceding element.The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p)    Some examples:    isMatch(“aa”,”a”) → false    isMatch(“aa”,”aa”) → true    isMatch(“aaa”,”aa”) → false    isMatch(“aa”, “a*”) → true    isMatch(“aa”, “.*”) → true    isMatch(“ab”, “.*”) → true    isMatch(“aab”, “c*a*b”) → true 
yesr
2019/03/14
2760
【每天一道编程系列-2018.3.11】—— A + B Problem
Write a function that add two numbers A and B. You should not use + or any arithmetic operators. 
yesr
2019/03/14
2490
【每天一道编程系列-2018.3.1】(Ans)
  Given an integer, convert it to a roman numeral.    Input is guaranteed to be within the range from 1 to 3999. 
yesr
2019/03/14
3720
【每天一道编程系列-2018.2.18】(Ans)
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 
yesr
2019/03/14
3080
【每天一道编程系列-2018.2.12】(Ans)
  Given an array of integers, find two numbers such that they add up to a specific target number.    The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.    You may assume that each input would have exactly one solution.    Input: numbers={2, 7, 11, 15}, target=9    Output: index1=1, index2=2 
yesr
2019/03/14
3330
【每天一道编程系列-2018.2.16】(Ans)
  Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1. 
yesr
2019/03/14
3480
【每天一道编程系列-2018.3.6】(Ans)
【题目描述】 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find
yesr
2019/03/14
3420
相关推荐
LintCode-2.尾部的零
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档