摘要:是的倍數,先找有多少個個,然后找多少個個,補上,然后多少個個,補上個個個
Problem
Write an algorithm which computes the number of trailing zeros in n factorial.
Challenge11! = 39916800, so the output should be 2
Notei是5的倍數,先找有多少個5(1個0),然后找多少個25(2個0),補上,然后多少個125(3個0),補上……
Solutionclass 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; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/65468.html
摘要:函數可解析數字或者字符串,并返回其整數部分。其中為可選參數,默認為進制。字符串首字符為數字字符串首字符為非數字和在對負數進行取整時,結果是有差異的。 原題目 Write a program that will calculate the number of trailing zeros in a factorial of a given number. http://mathworld...
摘要:迭代法復雜度時間空間思路技巧在于,每個數會產生一個。為什么呢試想,前個數中有一個一個,相乘有一個,后個數中有一個,又有一個。以此類推,每個數會有一個。代碼階乘中有多少,結果就有多少個 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...
閱讀 821·2023-04-26 00:37
閱讀 706·2021-11-24 09:39
閱讀 2132·2021-11-23 09:51
閱讀 3769·2021-11-22 15:24
閱讀 734·2021-10-19 11:46
閱讀 1868·2019-08-30 13:53
閱讀 2410·2019-08-29 17:28
閱讀 1314·2019-08-29 14:11