Skip to content

Instantly share code, notes, and snippets.

@wushbin
Created April 1, 2020 04:48
Show Gist options
  • Select an option

  • Save wushbin/e701d566890a1037c5cdf95eb2822a61 to your computer and use it in GitHub Desktop.

Select an option

Save wushbin/e701d566890a1037c5cdf95eb2822a61 to your computer and use it in GitHub Desktop.
class Solution {
public int findNthDigit(int n) {
long base = 1;
int cnt = 1;// cnt -> number of digits
while(n > 9 * base * cnt) {
n -= (9 * base * cnt);
cnt += 1;
base *= 10;
}
long num = (n - 1) / cnt + base;
String candidate = String.valueOf(num);
return candidate.charAt((n - 1) % cnt) - '0';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment