400_NthDigit - a920604a/leetcode GitHub Wiki
title: 400. Nth Digit
tags:
- Math
categories: leetcode
comments: false
solution
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';
}
};