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 = ; if (n >= && n <= )//0~9
{
var dif = n - ;
var shang = dif;
var yu = ; var basic = ( + shang).ToString();
result = Convert.ToInt32(basic.Substring(yu, ));
//9*1
}
else if (n >= && n <= )//10~99
{
var dif = n - ;
var shang = dif / ;
var yu = dif % ;
var next = ;
if (yu > )
{
next = ;
}
var basic = ( + shang + next).ToString();
if (yu > )
{
result = Convert.ToInt32(basic.Substring(yu - , ));
}
else
{
result = Convert.ToInt32(basic.Substring(basic.Length - , ));
}
//89*2
}
else if (n >= && n <= )//100~999
{
var dif = n - ;
var shang = dif / ;
var yu = dif % ;
var next = ;
if (yu > )
{
next = ;
}
var basic = ( + shang + next).ToString();
if (yu > )
{
result = Convert.ToInt32(basic.Substring(yu - , ));
}
else
{
result = Convert.ToInt32(basic.Substring(basic.Length - , ));
}
//899*3
}
else if (n >= && n <= )//1000~9999
{
var dif = n - ;
var shang = dif / ;
var yu = dif % ;
var next = ;
if (yu > )
{
next = ;
}
var basic = ( + shang + next).ToString();
if (yu > )
{
result = Convert.ToInt32(basic.Substring(yu - , ));
}
else
{
result = Convert.ToInt32(basic.Substring(basic.Length - , ));
}
//8999*4
}
else if (n >= && n <= )//10000~99999
{
var dif = n - ;
var shang = dif / ;
var yu = dif % ;
var next = ;
if (yu > )
{
next = ;
}
var basic = ( + shang + next).ToString();
if (yu > )
{
result = Convert.ToInt32(basic.Substring(yu - , ));
}
else
{
result = Convert.ToInt32(basic.Substring(basic.Length - , ));
}
//89999*5
}
else if (n >= && n <= )//100000~999999
{
var dif = n - ;
var shang = dif / ;
var yu = dif % ;
var next = ;
if (yu > )
{
next = ;
}
var basic = ( + shang + next).ToString();
if (yu > )
{
result = Convert.ToInt32(basic.Substring(yu - , ));
}
else
{
result = Convert.ToInt32(basic.Substring(basic.Length - , ));
}
//899999*6
}
else if (n >= && n <= )//1000000~9999999
{
var dif = n - ;
var shang = dif / ;
var yu = dif % ;
var next = ;
if (yu > )
{
next = ;
}
var basic = ( + shang + next).ToString();
if (yu > )
{
result = Convert.ToInt32(basic.Substring(yu - , ));
}
else
{
result = Convert.ToInt32(basic.Substring(basic.Length - , ));
}
//8999999*7
}
else if (n >= && n <= )//10000000~99999999
{
var dif = n - ;
var shang = dif / ;
var yu = dif % ;
var next = ;
if (yu > )
{
next = ;
}
var basic = ( + shang + next).ToString();
if (yu > )
{
result = Convert.ToInt32(basic.Substring(yu - , ));
}
else
{
result = Convert.ToInt32(basic.Substring(basic.Length - , ));
}
//89999999*8
}
else if (n >= && n <= int.MaxValue)//100000000~999999999
{
var dif = n - ;
var shang = dif / ;
var yu = dif % ;
var next = ;
if (yu > )
{
next = ;
}
var basic = ( + shang + next).ToString();
if (yu > )
{
result = Convert.ToInt32(basic.Substring(yu - , ));
}
else
{
result = Convert.ToInt32(basic.Substring(basic.Length - , ));
}
//899999999*9
} return result;
}
}

https://leetcode.com/problems/nth-digit/#/description

05-11 22:02