forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_400.java
29 lines (25 loc) · 843 Bytes
/
_400.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.fishercoder.solutions;
public class _400 {
public static class Solution1 {
/**
* credit: https://discuss.leetcode.com/topic/59314/java-solution:
* <p>
* 1. find the length of the number where the nth digit is from 2. find the actual number where
* the nth digit is from 3. find the nth digit and return
*/
public int findNthDigit(int n) {
int len = 1;
long count = 9;
int start = 1;
while (n > len * count) {
n -= len * count;
len += 1;
count *= 10;
start *= 10;
}
start += (n - 1) / len;
String s = Integer.toString(start);
return Character.getNumericValue(s.charAt((n - 1) % len));
}
}
}