400. Nth Digit

Problem

Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...

Note:
n is positive and will fit within the range of a 32-bit signed integer (n < 231).

Example

Input:
3

Output:
3
Input:
11

Output:
0

Code

static int var = [](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();
class Solution {
public:
    int findNthDigit(int n) {
        long i = 1;
        for(long count=9;n>i*count;i++,count*=10){
            n-=i*count; 
        }
        return to_string(pow(10,i-1)+(n-1)/i)[(n-1)%i]-'0';
    }
};

Result

400. Nth Digit.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 志工姓名:古春玲 搭档志工:王小芳 服务时间:2017年7月13号14点_16点, 地点:乌市天山区 服务对象:(...
    春回大地春玲阅读 310评论 0 0
  • 安琪,在上学期都给我留下来了深刻的印象了,为什么楠?因为她总是善解人意的那一个,事事都替别人着想。 特别是在去...
    欢欢小姑阅读 486评论 0 1
  • 毕业后一直为生活奔波,已经很少写点什么,人就像机器,失去了感悟的时间。是感悟不是矫情。我们都企图从生活工作中寻找,...
    蘇晚晚阅读 325评论 0 1