Skip to content

Commit 0d8cbf3

Browse files
committed
Add solution #400
1 parent 8d80750 commit 0d8cbf3

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@
318318
397|[Integer Replacement](./0397-integer-replacement.js)|Medium|
319319
398|[Random Pick Index](./0398-random-pick-index.js)|Medium|
320320
399|[Evaluate Division](./0399-evaluate-division.js)|Medium|
321+
400|[Nth Digit](./0400-nth-digit.js)|Medium|
321322
404|[Sum of Left Leaves](./0404-sum-of-left-leaves.js)|Easy|
322323
405|[Convert a Number to Hexadecimal](./0405-convert-a-number-to-hexadecimal.js)|Easy|
323324
407|[Trapping Rain Water II](./0407-trapping-rain-water-ii.js)|Hard|

solutions/0400-nth-digit.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 400. Nth Digit
3+
* https://leetcode.com/problems/nth-digit/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer n, return the nth digit of the infinite integer sequence
7+
* [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...].
8+
*/
9+
10+
/**
11+
* @param {number} n
12+
* @return {number}
13+
*/
14+
var findNthDigit = function(n) {
15+
let total = 1;
16+
let start = 1;
17+
18+
for (let count = 9; n > total * count; total++, count *= 10, start *= 10) {
19+
n -= total * count;
20+
}
21+
22+
start += Math.floor((n - 1) / total);
23+
return +(start.toString()[(n - 1) % total]);
24+
};

0 commit comments

Comments
 (0)