成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode/LintCode] Factorial Trailing Zeros

Java_oldboy / 682人閱讀

摘要:是的倍數,先找有多少個個,然后找多少個個,補上,然后多少個個,補上個個個

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的倍數,先找有多少個5(1個0),然后找多少個25(2個0),補上,然后多少個125(3個0),補上……

Solution
class Solution {
    public long trailingZeros(long n) {
        long i = 5;
        long count = 0;
        while (i <= n) { 
        //n = 125, i = 5, count = 25; 25個5 
        //i = 25, count += 5(5個25)= 30; i = (1個)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;
    }
}

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

轉載請注明本文地址:http://systransis.cn/yun/65468.html

相關文章

  • 【5 kyu】計算N的階乘末尾幾個0,Number of trailing zeros of N!

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

    beanlam 評論0 收藏0
  • [Leetcode] Factorial Trailing Zeroes 末尾零

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

    qpwoeiru96 評論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 評論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 評論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 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<