Skip to content

Commit f81c272

Browse files
authored
Update 400. Nth Digit.java
1 parent c37d27d commit f81c272

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

400. Nth Digit.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,15 @@ public int findNthDigit(int n) {
3030
}
3131
}
3232
}
33+
34+
// Space and Time improvement https://discuss.leetcode.com/topic/59378/short-python-java
35+
public int findNthDigit(int n) {
36+
n -= 1;
37+
int digits = 1, first = 1;
38+
while (n / 9 / first / digits >= 1) {
39+
n -= 9 * first * digits;
40+
digits++;
41+
first *= 10;
42+
}
43+
return (first + n/digits + "").charAt(n%digits) - '0';
44+
}

0 commit comments

Comments
 (0)