美文网首页
2022-02-22 剑指 Offer 44. 数字序列中某

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

作者: 16孙一凡通工 | 来源:发表于2022-02-23 09:29 被阅读0次

数学题
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');
}

相关文章

网友评论

      本文标题:2022-02-22 剑指 Offer 44. 数字序列中某

      本文链接:https://www.haomeiwen.com/subject/bsfglrtx.html