Skip to content

Instantly share code, notes, and snippets.

@linfongi
Last active February 16, 2017 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linfongi/092fc39b102f7f7e8c62e0f0a0044e1a to your computer and use it in GitHub Desktop.
Save linfongi/092fc39b102f7f7e8c62e0f0a0044e1a to your computer and use it in GitHub Desktop.
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