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

数字序列中某一位的数字

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

//吃数学的亏
 int findNthDigit(int n) {
        // write code here
        int dig  =1;
   		//start 用long long 否则越界结果不对。
        long long start = 1,count = 9;
        while(n>count)
        //原本考虑top-bottom,但是0-9和后面的10-99、100-999有点出入
        {
            n-=count;
            start*=10;
            dig++;
            count = start*dig*9;
        }
        int num = start + (n-1)/dig;//n-1让下标从0开始,方便求商
        return to_string(num)[(n-1)%dig]-'0';//可以不用细想,直接认为是求当前元素中的下标
    }
全部评论

相关推荐

不愿透露姓名的神秘牛友
06-23 18:30
点赞 评论 收藏
分享
水墨不写bug:疑似没有上过大学
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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