-
Notifications
You must be signed in to change notification settings - Fork 0
400_NthDigit
a920604a edited this page Apr 14, 2023
·
1 revision
class Solution {
public:
int findNthDigit(int n) {
long long len = 1, cnt = 9, start = 1;
// 先定位要去哪個區間找尋答案 [1,9] [10,99] [100,999] [1000,9999]
// 1個位數*9個 2個位數*90個 ...
while(n> len*cnt){
n-= len*cnt;
len++;
cnt *=10;
start *=10;
}
start += (n-1)/len;
// 答案在這數字裡
string t = to_string(start);
return t[(n-1)%len]-'0';
}
};
footer