弑天下
你的程序非常非常低效。user1431317 的程序效率很低。简单地计算价值。它只需要几纳秒的 CPU 时间和一些内存分配,即使是大到 9,223,372,036,854,775,807 的数字索引(95.6 纳秒和我的计算机上的 2 次分配)。例如,package mainimport ( "fmt" "math" "strconv")// digit returns the ith digit from the sequence of// concatenated non-negative integers.// The sequence of digits is 01234567891011121314151617181920...func digit(i int64) string { // There are 9 one digit positive integers, 90 two digit, // 900 three digit, and so on. if i <= 0 { return "0" } j := int64(1) w := 1 for ; ; w++ { t := j + 9*int64(math.Pow10(w-1))*int64(w) if 0 > t || t > i { break } j = t } k := i - j n := k / int64(w) m := k % int64(w) d := strconv.FormatInt(int64(math.Pow10(w-1))+n, 10)[m] return string(d)}func main() { tests := []int64{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 88, 89, 188, 189, 190, 191, 192, math.MaxInt32, math.MaxInt64, } for _, n := range tests { fmt.Println(n, digit(n)) }}输出:0 01 12 23 34 45 56 67 78 89 910 111 012 113 188 489 9188 9189 9190 1191 0192 02147483647 29223372036854775807 9