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

leetcode400的更多相关文章

  1. [Swift]LeetCode400. 第N个数字 | Nth Digit

    Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note:n is ...

随机推荐

  1. HDU-4336-期望dp-bit

    Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. (UDP)socket编程

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  3. JDK1.7+eclipse 4.4(luna)+pydev4.4.5构建django开发环境

    最近一直用pycharm搞django学习,但是到2017年随着版本的不断更新,启动之慢,吃资源吃内存越来越严重.果然想找一个IDE替代品. 之前用java开发分布式WEB应用,用eclipse开了N ...

  4. kindEditor富文本编辑器

    用法参考:http://kindeditor.net/docs/usage.html  一.使用 . 修改HTML页面 在需要显示编辑器的位置添加textarea输入框. <textarea i ...

  5. PostgreSQL常用插件收集

    hexdump -C 数据表文件 -- 查看表文件中数据. pg_stat_statements pgcompacttable -- 在减少锁的情况下,清理表和索引的老空间. pg_repack--P ...

  6. VScode+Flutter 开发继续踩坑

    运行慢解决方法1:修改build.gradle,注释掉jcenter(),google().使用阿里的镜像.原因是jcenter google库无法访问到导致的问题.虽然我有万能的爬墙工具,开启全局代 ...

  7. scikit-learn 学习笔记-- Generalized Linear Models (二)

    Lasso regression 今天介绍另外一种带正则项的线性回归, ridge regression 的正则项是二范数,还有另外一种是一范数的,也就是lasso 回归,lasso 回归的正则项是系 ...

  8. CentOS 6&7安装ffmpeg

    CentOS 6和7安装方法是不一样的,下面分别说明: 安装前都需要先安装epel扩展源 yum -y install epel-release CentOS 6比较简单,安装yum源之后直接安装即可 ...

  9. spring-security-4 (3)spring security过滤器的创建与注册原理

    spring security是通过一个过滤器链来保护你的web应用安全.在spring security中,该过滤链的名称为springSecurityFilterChain,类型为FilterCh ...

  10. python requests 设置headers 和 post请求体x-www-form-urlencoded

    1.application/json:是JSON格式提交的一种识别方式.在请求头里标示.2.application/x-www-form-urlencoded : 这是form表单提交的时候的表示方式 ...