日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Swift]LeetCode793. 阶乘函数后K个零 | Preimage Size of Factorial Zeroes Function

發布時間:2024/4/17 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Swift]LeetCode793. 阶乘函数后K个零 | Preimage Size of Factorial Zeroes Function 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號:山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:?https://www.cnblogs.com/strengthen/p/10547060.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強烈建議點擊原文地址閱讀!支持作者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Let?f(x)?be the number of zeroes at the end of?x!. (Recall that?x! = 1 * 2 * 3 * ... * x, and by convention,?0! = 1.)

For example,?f(3) = 0?because 3! = 6 has no zeroes at the end, while?f(11) = 2?because 11! = 39916800 has 2 zeroes at the end. Given?K, find how many non-negative integers?xhave the property that?f(x) = K.

Example 1: Input: K = 0 Output: 5 Explanation: 0!, 1!, 2!, 3!, and 4! end with K = 0 zeroes.Example 2: Input: K = 5 Output: 0 Explanation: There is no x such that x! ends in K = 5 zeroes.

Note:

  • K?will be an integer in the range?[0, 10^9].

?f(x)?是?x!?末尾是0的數量。(回想一下?x! = 1 * 2 * 3 * ... * x,且0! = 1)

例如,?f(3) = 0?,因為3! = 6的末尾沒有0;而?f(11) = 2?,因為11!= 39916800末端有2個0。給定?K,找出多少個非負整數x?,有?f(x) = K?的性質。

示例 1: 輸入:K = 0 輸出:5 解釋:?0!, 1!, 2!, 3!, and 4!?均符合 K = 0 的條件。示例 2: 輸入:K = 5 輸出:0 解釋:沒有匹配到這樣的 x!,符合K = 5 的條件。

注意:

  • K是范圍在?[0, 10^9]?的整數。


Runtime:?4 ms Memory Usage:?18.7 MB 1 class Solution { 2 func preimageSizeFZF(_ K: Int) -> Int { 3 if K < 5 {return 5} 4 var base:Int = 1 5 while(base * 5 + 1 <= K) 6 { 7 base = base * 5 + 1 8 } 9 if K / base == 5 10 { 11 return 0 12 } 13 return preimageSizeFZF(K % base) 14 } 15 }

4ms

1 class Solution { 2 func preimageSizeFZF(_ K: Int) -> Int { 3 var start = 1, end = Int.max, mid = start + (end - start) / 2 4 5 while start < end { 6 let candidate = trailingZeroes(mid) 7 if candidate > K { 8 end = mid - 1 9 } else if candidate < K { 10 start = mid + 1 11 } else { 12 return 5 13 } 14 mid = start + (end - start) / 2 15 } 16 return 0 17 } 18 19 func trailingZeroes(_ n: Int) -> Int { 20 var n = n, current = 0 21 while n > 1 { 22 n /= 5 23 current += n 24 } 25 26 return current 27 } 28 }

?

轉載于:https://www.cnblogs.com/strengthen/p/10547060.html

總結

以上是生活随笔為你收集整理的[Swift]LeetCode793. 阶乘函数后K个零 | Preimage Size of Factorial Zeroes Function的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。