public class Solution {
public int FindNthDigit(int n)
{
//StringBuilder sb = new StringBuilder();
//for (int i = 1; i < int.MaxValue; i++)
//{
// if (sb.Length < n)
// {
// sb.Append(i);
// }
// else
// {
// break;
// }
//}
//var k = sb.Length;
//var s = sb.ToString().Substring(n - 1, 1);
//var result = Convert.ToInt32(s);
//return result;
var result = 0;
if (n >= 1 && n <= 9)//0~9
{
var dif = n - 0;
var shang = dif;
var yu = 0;
var basic = (0 + shang).ToString();
result = Convert.ToInt32(basic.Substring(yu, 1));
//9*1
}
else if (n >= 10 && n <= 189)//10~99
{
var dif = n - 9;
var shang = dif / 2;
var yu = dif % 2;
var next = 0;
if (yu > 0)
{
next = 1;
}
var basic = (9 + shang + next).ToString();
if (yu > 0)
{
result = Convert.ToInt32(basic.Substring(yu - 1, 1));
}
else
{
result = Convert.ToInt32(basic.Substring(basic.Length - 1, 1));
}
//89*2
}
else if (n >= 190 && n <= 2889)//100~999
{
var dif = n - 189;
var shang = dif / 3;
var yu = dif % 3;
var next = 0;
if (yu > 0)
{
next = 1;
}
var basic = (99 + shang + next).ToString();
if (yu > 0)
{
result = Convert.ToInt32(basic.Substring(yu - 1, 1));
}
else
{
result = Convert.ToInt32(basic.Substring(basic.Length - 1, 1));
}
//899*3
}
else if (n >= 2900 && n <= 38889)//1000~9999
{
var dif = n - 2889;
var shang = dif / 4;
var yu = dif % 4;
var next = 0;
if (yu > 0)
{
next = 1;
}
var basic = (999 + shang + next).ToString();
if (yu > 0)
{
result = Convert.ToInt32(basic.Substring(yu - 1, 1));
}
else
{
result = Convert.ToInt32(basic.Substring(basic.Length - 1, 1));
}
//8999*4
}
else if (n >= 39000 && n <= 488889)//10000~99999
{
var dif = n - 38889;
var shang = dif / 5;
var yu = dif % 5;
var next = 0;
if (yu > 0)
{
next = 1;
}
var basic = (9999 + shang + next).ToString();
if (yu > 0)
{
result = Convert.ToInt32(basic.Substring(yu - 1, 1));
}
else
{
result = Convert.ToInt32(basic.Substring(basic.Length - 1, 1));
}
//89999*5
}
else if (n >= 490000 && n <= 5888889)//100000~999999
{
var dif = n - 488889;
var shang = dif / 6;
var yu = dif % 6;
var next = 0;
if (yu > 0)
{
next = 1;
}
var basic = (99999 + shang + next).ToString();
if (yu > 0)
{
result = Convert.ToInt32(basic.Substring(yu - 1, 1));
}
else
{
result = Convert.ToInt32(basic.Substring(basic.Length - 1, 1));
}
//899999*6
}
else if (n >= 5900000 && n <= 68888889)//1000000~9999999
{
var dif = n - 5888889;
var shang = dif / 7;
var yu = dif % 7;
var next = 0;
if (yu > 0)
{
next = 1;
}
var basic = (999999 + shang + next).ToString();
if (yu > 0)
{
result = Convert.ToInt32(basic.Substring(yu - 1, 1));
}
else
{
result = Convert.ToInt32(basic.Substring(basic.Length - 1, 1));
}
//8999999*7
}
else if (n >= 69000000 && n <= 788888889)//10000000~99999999
{
var dif = n - 68888889;
var shang = dif / 8;
var yu = dif % 8;
var next = 0;
if (yu > 0)
{
next = 1;
}
var basic = (9999999 + shang + next).ToString();
if (yu > 0)
{
result = Convert.ToInt32(basic.Substring(yu - 1, 1));
}
else
{
result = Convert.ToInt32(basic.Substring(basic.Length - 1, 1));
}
//89999999*8
}
else if (n >= 790000000 && n <= int.MaxValue)//100000000~999999999
{
var dif = n - 788888889;
var shang = dif / 9;
var yu = dif % 9;
var next = 0;
if (yu > 0)
{
next = 1;
}
var basic = (99999999 + shang + next).ToString();
if (yu > 0)
{
result = Convert.ToInt32(basic.Substring(yu - 1, 1));
}
else
{
result = Convert.ToInt32(basic.Substring(basic.Length - 1, 1));
}
//899999999*9
}
return result;
}
}
https://leetcode.com/problems/nth-digit/#/description