2022-02-22 剑指 Offer 44. 数字序列中某

2022-02-23  本文已影响0人  16孙一凡通工

数学题
Go版本:

import (
    "strconv"
)
func findNthDigit(n int) int {


    // 0-9    1  9个
    // 10-99  二  90
    // 100-999  3  900
    // 数学找规律的题目
    if(n<10){
        return n;
    }
    //    
    border,start,loc:=9,1,1;
    for n>border{
       
    n-=border;
    start++;
    loc=loc*10;
    border=start*9*loc;
    }
    num:=loc+(n-1)/start;
    num_str:=strconv.Itoa(num)
     index := (n-1)%start
    return int(num_str[index]-'0');
}
上一篇 下一篇

猜你喜欢

热点阅读