摘要:是的倍數(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.
Challenge11! = 39916800, so the output should be 2
Notei是5的倍數(shù),先找有多少個(gè)5(1個(gè)0),然后找多少個(gè)25(2個(gè)0),補(bǔ)上,然后多少個(gè)125(3個(gè)0),補(bǔ)上……
Solutionclass 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
摘要:函數(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...
摘要:迭代法復(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 ...
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...
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) ...
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...
閱讀 935·2023-04-26 00:37
閱讀 802·2021-11-24 09:39
閱讀 2235·2021-11-23 09:51
閱讀 3975·2021-11-22 15:24
閱讀 804·2021-10-19 11:46
閱讀 1918·2019-08-30 13:53
閱讀 2508·2019-08-29 17:28
閱讀 1401·2019-08-29 14:11