Last active
February 16, 2017 06:46
-
-
Save linfongi/092fc39b102f7f7e8c62e0f0a0044e1a to your computer and use it in GitHub Desktop.
This file contains 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
public class Solution { | |
public int findNthDigit(int n) { | |
int digitCount = 1; | |
long threshold = 9 * (long)Math.pow(10, digitCount - 1) * digitCount; | |
while (n > threshold) { | |
n -= threshold; | |
threshold /= digitCount; | |
threshold *= 10 * (++digitCount); | |
} | |
final int idx = (n - 1) % digitCount; | |
final int num = (int)Math.pow(10, digitCount - 1) + (n - 1) / digitCount; | |
return Character.getNumericValue(String.valueOf(num).charAt(idx)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment