Question

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).

Example 1:

  1. Input:
  2. 3
  3. Output:
  4. 3

Example 2:

  1. Input:
  2. 11
  3. Output:
  4. 0
  5. Explanation:
  6. 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

Solution

就是统计位数,然后注意越界的问题,所以应该用long.

Code

  1. class Solution {
  2. public:
  3. int findNthDigit(int n) {
  4. if (n < 10)
  5. return n;
  6. int i = 1;
  7. long pre = 0;
  8. while (1) {
  9. long value = i * 9 * pow(10, i - 1);
  10. if (n > value) {
  11. pre += value;
  12. i++;
  13. }
  14. else {
  15. break;
  16. }
  17. }
  18. long remain = n - pre;
  19. long y = remain / i;
  20. long z = remain % i;
  21. i--;
  22. long start;
  23. if (z > 0)
  24. start = pow(10, i) + y;
  25. else
  26. start = pow(10, i) + y - 1;
  27. stringstream ss;
  28. ss << start;
  29. string str;
  30. ss >> str;
  31. if (z > 0)
  32. return (str[z - 1]) - 48;
  33. else
  34. return (str[str.length() - 1]) - 48;
  35. }
  36. };

LeetCode——Nth Digit的更多相关文章

  1. [LeetCode] Nth Digit 第N位

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

  2. Leetcode: Nth Digit

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

  3. C++版 - Leetcode 400. Nth Digit解题报告

    leetcode 400. Nth Digit 在线提交网址: https://leetcode.com/problems/nth-digit/ Total Accepted: 4356 Total ...

  4. 【LeetCode】400. Nth Digit 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. Nth Digit | leetcode

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

  6. leetcode 400 Add to List 400. 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 ...

  7. [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 ...

  8. hdu 1597 find the nth digit

    find the nth digit Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. find the nth digit(二分查找)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1597 find the nth digit Time Limit: 1000/1000 MS (Jav ...

随机推荐

  1. linux文件与目录管理命令(ubuntu)

    ls:列出目录 选项与参数: -a:全部文件,隐藏档(开头为.的文件)也会列出: -d:仅列出目录本身(也就是 . ),而不是目录下的所有文件及目录: -l:长字符串列出,包括文件的属性.权限等数据.

  2. TuShare获取K线数据

    Tushare是一个免费.开源的python财经数据接口包.主要实现对股票等金融数据从数据采集.清洗加工 到 数据存储的过程,能够为金融分析人员提供快速.整洁.和多样的便于分析的数据,为他们在数据获取 ...

  3. TypeError: cannot use a string pattern on a bytes-like object的解决办法

    #!/usr/python3 import re import urllib.request def gethtml(url): page=urllib.request.urlopen(url) ht ...

  4. luarocks安装以及lfs安装

    一.先安装lua: brew install lua 我本机的安装路径为:/usr/local/Cellar/lua/5.3.4_2 二.安装luarocks 下载luarocks的安装包: http ...

  5. 移动支付--银联,支付宝,微信(android)

    在这个移动互联网快速发展的时代,手机已经成为人们生活或者出行之中必不可少的设备了,如今非常多城市的商户都能够採用支付宝,微信支付了.人们出门仅仅须要随身携带带手机.不用带大量现金就能够放心购物了.如今 ...

  6. SDUT3143:Combinatorial mathematics(组合数学)

    题意:传送门 题目描述 As you know, shadow95 is pretty good at maths, especially combinatorial mathematics. Now ...

  7. 把typora改为微软雅黑+Consolas

    前言 typora是一款非常方便的书写markdown文本的编辑器.官网:https://www.typora.io/ 对于字体强迫症患者来说,不把字体改成微软雅黑+Consolas,那是相当难受.本 ...

  8. 《Oracle RAC性能优化》

    一 RAC环境 RAC架构,2节点信息 节点1 SQL> show parameter instance NAME                                 TYPE    ...

  9. matplotlib绘制散点图

    参考自Matplotlib Python 画图教程 (莫烦Python)(10)_演讲•公开课_科技_bilibili_哔哩哔哩 https://www.bilibili.com/video/av16 ...

  10. Windows常见宏的使用

    WIN32_LEAN_AND_MEAN 1.  参考资料:https://msdn.microsoft.com/en-us/library/windows/desktop/aa383745(v=vs. ...