问题

在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 个数字。

注意:n 是正数且在32为整形范围内 ( n < 231)。

输入:3

输出:3

输入:11

输出:0

说明:第11个数字在序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... 里是0,它是10的一部分。


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

Note:n is positive and will fit within the range of a 32-bit signed integer (n < 231).

Input:3

Output:3

Input:11

Output:0

Explanation:The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which is part of the number 10.


示例

public class Program {public static void Main(string[] args) {var n = 15;var res = FindNthDigit(n);Console.WriteLine(res);n = 38;res = FindNthDigit2(n);Console.WriteLine(res);n = 168;res = FindNthDigit3(n);Console.WriteLine(res);Console.ReadKey();}private static int FindNthDigit(int n) {//暴力求解,此解法LeetCode超时未ACvar res = 0;var num = 1;while(res < n) {res += num.ToString().Length;num++;}num--;res = int.Parse(num.ToString()[num.ToString().Length - (res - n) - 1].ToString());return res;}private static int FindNthDigit2(int n) {int digitType = 1;long digitNum = 9;//分析n是几位while(n > digitNum * digitType) {n -= (int)digitNum * digitType;digitType++;digitNum *= 10;}//第几个第几位int indexInSubRange = (n - 1) / digitType;int indexInNum = (n - 1) % digitType;//输出结果int num = (int)Math.Pow(10, digitType - 1) + indexInSubRange;int result = int.Parse(("" + num)[indexInNum] + "");return result;}private static int FindNthDigit3(int n) {long start = 1, sz = 1, bace = 9;while(sz * bace < n) {n -= (int)(sz * bace);bace *= 10;++sz;start *= 10;}int target = (int)(start + (n - 1) / sz);return target.ToString()[(n - 1) % (int)sz] - '0';}}

以上给出3种算法实现,以下是这个案例的输出结果:

False
False
True

分析:

显而易见,IsPowerOfThree 的时间复杂度为:  ,IsPowerOfThree2 和IsPowerOfThree3 的时间复杂度为:  。

C#LeetCode刷题之#400-第N个数字(Nth Digit)相关推荐

  1. C#LeetCode刷题之#747-至少是其他数字两倍的最大数( Largest Number At Least Twice of Others)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3746 访问. 在一个给定的数组nums中,总是存在一个最大元素 ...

  2. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  3. ​LeetCode刷题实战276:栅栏涂色

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

  4. C#LeetCode刷题-数学

    数学篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 (a ...

  5. LeetCode刷题1:第四周

    LeetCode刷题1:第四周 目录 LeetCode刷题1:第四周 一.前言 二.知识点 1.Python 字符串 Python 访问字符串中的值 Python 字符串更新 Python转义字符 P ...

  6. LeetCode刷题专栏第一篇--思维导图时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  7. LeetCode刷题指南——题目精选1

    这是LeetCode经典题目总结文章~ 基础:将数据结构及算法学习的差不多,LeetCode题目按类别刷题及总结,参考鄙人数据结构及算法系列文章~ 按类别将每类题目做好,大概刷250道左右的程度即可. ...

  8. LeetCode刷题笔记(算法思想 四)

    LeetCode刷题笔记(算法思想 四) 七.动态规划 斐波那契数列 70. 爬楼梯 198. 打家劫舍 213. 打家劫舍 II 信件错排 母牛生产 矩阵路径 64. 最小路径和 62. 不同路径 ...

  9. Leetcode-How-What 力扣Leetcode刷题指南

    Leetcode-How-What 力扣Leetcode刷题指南 About the way how to use Leetcode wisely for preparing the intervie ...

  10. LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)

    LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...

最新文章

  1. 飞得更高:(三)人不好招啊
  2. 动态多维数组在 VC 中的应用
  3. 第十章—DOM(0)—NODE类型
  4. .NET Core 使用 nlog 进行日志记录
  5. 如何用linux登入数据库,Linux中MySQL数据库的使用①-----登录、数据库操作、表结构操作...
  6. ios php 表单提交图片上传,axios发送post请求提交图片表单步骤详解
  7. [html] 说说页面中字体渲染规则是怎样的?会有哪些因素影响字体的渲染?
  8. Linux压缩与解压常用命令
  9. mysql数据库的链接地址_常用数据库连接URL地址大全
  10. 1.4.2.4. SAVING(Core Data 应用程序实践指南)
  11. es6 混合commjs_Webpack打包ES6和CommonJs混合React
  12. 携号转网不会有多大影响
  13. python判断是否含有0_Python:判断文本中的用户名在数据库中是否存在,存在返回1,不存在返回0...
  14. tinymce富文本编辑器(vue)
  15. 分类模型的评价指标--混淆矩阵,ROC曲线,AUC及sklearn.metrics.roc_curve参数说明
  16. 浅谈霍尔电流传感器的原理及应用
  17. (便携式投影仪) FP7209补光灯调光芯片无频闪无抖动 LED升压恒流芯片最大驱动300W
  18. javaweb实现支付宝扫码支付完整流程
  19. 刷脸支付是新的创业风口,刷脸支付推广线下商户策略!干货分享!
  20. 介绍几款最优秀的CMS(网站内容管理系统)

热门文章

  1. BinarySearch 有序表的二分查找
  2. Linux tail命令:显示文件结尾的内容
  3. 【python】sys.stdout输出不换行
  4. 【今日CV 计算机视觉论文速览】Thu, 7 Mar 2019
  5. java游戏循环 限定次数的游戏
  6. 解决 阶段02 商品类与初始商品
  7. celery-04-操作b-执行人的一方
  8. python-函数的位置参数
  9. springboot(四)——@EnableConfigurationProperties是如何起作用的你知道吗
  10. 本地程序部署服务器上出错