Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created February 20, 2018 14:50
Show Gist options
  • Save s4553711/e46404d02fcd47cddda248e34e5d4c6f to your computer and use it in GitHub Desktop.
Save s4553711/e46404d02fcd47cddda248e34e5d4c6f to your computer and use it in GitHub Desktop.
class Solution {
public:
int findNthDigit(int n) {
long digit = 1, sum = 9;
while(n > digit * sum) {
n = n - digit * sum;
sum *= 10;
digit++;
}
int index = n % digit;
if (index == 0) {
index = digit;
}
long num = pow(10, digit - 1);
num += (index == digit) ? (n/digit - 1) : (n/digit);
for(int i = index; i < digit; i++) {
num /= 10;
}
return num % 10;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment