亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

[LeetCode/LintCode] Factorial Trailing Zeros

Java_oldboy / 724人閱讀

摘要:是的倍數(shù),先找有多少個(gè)個(gè),然后找多少個(gè)個(gè),補(bǔ)上,然后多少個(gè)個(gè),補(bǔ)上個(gè)個(gè)個(gè)

Problem

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

Challenge

11! = 39916800, so the output should be 2

Note

i是5的倍數(shù),先找有多少個(gè)5(1個(gè)0),然后找多少個(gè)25(2個(gè)0),補(bǔ)上,然后多少個(gè)125(3個(gè)0),補(bǔ)上……

Solution
class Solution {
    public long trailingZeros(long n) {
        long i = 5;
        long count = 0;
        while (i <= n) { 
        //n = 125, i = 5, count = 25; 25個(gè)5 
        //i = 25, count += 5(5個(gè)25)= 30; i = (1個(gè))125, count  += 1 = 31; 
            count += n / i;
            i = i * 5;
        }
        return count;
    }
}
LeetCode version
class Solution {
    public int trailingZeroes(int n) {
        int count = 0;
        while (n > 0) {
            count += n/5;
            n /= 5;
        }
        return count;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/65468.html

相關(guān)文章

  • 【5 kyu】計(jì)算N的階乘末尾幾個(gè)0,Number of trailing zeros of N!

    摘要:函數(shù)可解析數(shù)字或者字符串,并返回其整數(shù)部分。其中為可選參數(shù),默認(rèn)為進(jìn)制。字符串首字符為數(shù)字字符串首字符為非數(shù)字和在對(duì)負(fù)數(shù)進(jìn)行取整時(shí),結(jié)果是有差異的。 原題目 Write a program that will calculate the number of trailing zeros in a factorial of a given number. http://mathworld...

    beanlam 評(píng)論0 收藏0
  • [Leetcode] Factorial Trailing Zeroes 末尾零

    摘要:迭代法復(fù)雜度時(shí)間空間思路技巧在于,每個(gè)數(shù)會(huì)產(chǎn)生一個(gè)。為什么呢試想,前個(gè)數(shù)中有一個(gè)一個(gè),相乘有一個(gè),后個(gè)數(shù)中有一個(gè),又有一個(gè)。以此類推,每個(gè)數(shù)會(huì)有一個(gè)。代碼階乘中有多少,結(jié)果就有多少個(gè) Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...

    qpwoeiru96 評(píng)論0 收藏0
  • [LeetCode/LintCode] Top K Frequent Words

    LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...

    0x584a 評(píng)論0 收藏0
  • [LeetCode/LintCode] Is Subsequence

    Problem Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) ...

    terasum 評(píng)論0 收藏0
  • [LeetCode/LintCode] Odd Even Linked List

    Problem Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. Example Example:Given...

    awokezhou 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<