题解 | #数字序列中某一位的数字#

数字序列中某一位的数字

https://www.nowcoder.com/practice/29311ff7404d44e0b07077f4201418f5

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param n int整型 
 * @return int整型
 */
export function findNthDigit(n: number): number {
    // write code here
    let  digit = 1
    let  start = 0
    let end = 10
    while(n > (end - start) * digit){
        n -= (end - start) * digit
        digit += 1
        start = end
        end = end *10
    }
    //找到指定的那个数
    const num = Math.ceil((n)/digit) + start
    const index = n % digit
    return Number(num.toString()[index])
}

全部评论

相关推荐

点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
08-08 18:20
职场水母:这题思路是什么,我目前想的一个暴力方法就是先把这个链表遍历一遍,用哈希表存储出现次数,然后再根据哈希表来一个一个删除节点,
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务