Created
February 20, 2018 14:50
-
-
Save s4553711/e46404d02fcd47cddda248e34e5d4c6f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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