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: 3 Output: 3
Example 2:
Input: 11 Output: 0 Explanation: 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.
public class Solution {
public int findNthDigit(int n) {
int i=1; //i表示数字位数,比如数字123是3位
int m=n;
int num=0; //第nth所属数字
int bit=0; //第n位位于所属数字的第bit位(从左数)
if(n<10) return n;
while(n>i*9*Math.pow(10,i-1))//数字位数为1的有9个,2的有90个,3的有900个。。。
{
m-=i*9*Math.pow(10,i-1);
i++;
}
num=(int)((m-1)/i+Math.pow(10,i-1));
bit=(m-1)%i+1;
while(bit!=i)
{
num=num/10;
i--;
}
return num%10;
}
}