問題
例子
思路
-
方法1
$$$$
- 確定位數【i位數共有多少位數字:10*(i-1)9i】
- 確定所在的數字
- 確定是哪一位數
-
方法2
$$$$
代碼
//方法1
class Solution {
public int findNthDigit(int n) {
//所在數字是i位
int i=1;
//當n爲10^9時,區間最大值>Integer.MAX_VALUE
while(n > (long)Math.pow(10,i-1)*9*i) {
n-=(long)Math.pow(10,i-1)*9*i;
i++;
}
//是哪一個數字 10^(i-1)是i位數的起始數字 如:2位數 10
//n爲第幾個數,n-1爲下標,(n-1)/i 數坐在的下標 (n-1)%i在i長度中的下標
long num = (int)Math.pow(10,i-1)+(n-1)/i;
String s = String.valueOf(num);
return s.charAt((n-1)%i)-'0';
}
}
//方法2