在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Let For example, 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:
例如, 示例 1: 输入:K = 0 输出:5 解释: 0!, 1!, 2!, 3!, and 4! 均符合 K = 0 的条件。 示例 2: 输入:K = 5 输出:0 解释:没有匹配到这样的 x!,符合K = 5 的条件。 注意:
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 }
|
请发表评论