解題思路
首先,我們要明確的是,n是下標,從0開始的!
我們可以注意到規律 0到9有10個數字,10到99有90個數字,100到999有900個數字,so~
代碼
class Solution {
public int findNthDigit(int n) {
if (n<10)
return n;
int i = 1;
while (n>i*(Math.pow(10,i-1))*9){ //循環結束後,i-1就是位數,n-1爲表示還要找多少個
n -= i*Math.pow(10,i-1)*9;
i++;
}
char[] result = String.valueOf((int) Math.pow(10,i-1) + (n-1) / i).toCharArray();//我們用字符串來接收值,方便找位數 result結果爲我們要的那個數的
int value = result[(n-1)%i]-'0'; //(n-1)%位數 得出我們要的第x位的數
return value;
}
}