-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution400.cs
30 lines (26 loc) · 883 Bytes
/
Solution400.cs
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
30
namespace LeetCode.Solutions;
public class Solution400
{
/// <summary>
/// 400. Nth Digit - Easy
/// <a href="https://leetcode.com/problems/nth-digit">See the problem</a>
/// </summary>
public int FindNthDigit(int n)
{
long digitLength = 1; // the number of digits in the current range
long count = 9; // the count of numbers in the current range
long start = 1; // the starting number of the current range
while (n > digitLength * count)
{
n -= (int)(digitLength * count);
digitLength++;
count *= 10;
start *= 10;
}
// Step 2: Find the exact number
start += (n - 1) / digitLength;
string s = start.ToString();
// Step 3: Find the exact digit
return s[(n - 1) % (int)digitLength] - '0';
}
}