Created
April 1, 2020 04:48
-
-
Save wushbin/e701d566890a1037c5cdf95eb2822a61 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 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