Me(AC 33 / 44 個通過測試用例)
func preimageSizeFZF(K int) int {count := 0for i := 0; i < 1000000; i++ {if trailingZeroes(i) == K {count++}}fmt.Println(count)//fmt.Println(trailingZeroes(25))return count}func trailingZeroes(n int) int {if n == 0 {return 0}return trailingZeroes(n / 5) + n / 5
}
強行通過后
這個題答案是使用二分查找減少時間。1136ms已經是非常少見的,而且擊敗了100%的用戶。
執行用時 :1136 ms, 在所有 Go 提交中擊敗了100.00%的用戶
內存消耗 :1.9 MB, 在所有 Go 提交中擊敗了50.00%的用戶
使用并發大法(本地通過)
var count int = 0func preimageSizeFZF(K int) int {wg := sync.WaitGroup{}wg.Add(1000000)for i := 0; i < 1000000; i++ {go func(i int) {trailingZeroes(i, K)wg.Done()}(i)}return count
}func trailingZeroes(i int, K int) {if trailingZeroes1(i) == K {count++}
}func trailingZeroes1(n int) int {if n == 0 {return 0}return trailingZeroes1(n / 5) + n / 5
}
執行結果: 超出內存限制