400. Nth Digit

来源:互联网 发布:js怎么获取对象的长度 编辑:程序博客网 时间:2024/04/20 01:10

题目:

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 1:

Input:3Output:3

Example 2:

Input:11Output:0Explanation:The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which is part of the number 10.
思路:

本题题意比较难以理解,看了官方的solution才发现了题目的含义:寻找n是整数序列中的第几个数字(digit),是数字所以1-10,一共有11个数字,这样理解了题目的目的后,解题思路就有了,参考主流方法,分为三步:

step1:找到n落在哪个位数区间1-9;10-99;100-999;.....

step2:找到n落在哪个数值上

step3:找到n落在哪个位上

代码:

class Solution {public:    int findNthDigit(int n) {        long digit = 1, ith = 1, base = 9;          while(n > base*digit)          {              n -= base*(digit++);              ith += base;              base *= 10;          }          return to_string(ith+(n-1)/digit)[(n-1)%digit]-'0';              }};


原创粉丝点击