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:

Input:
3 Output:
3

Example 2:

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

Solution

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

Code

class Solution {
public:
int findNthDigit(int n) {
if (n < 10)
return n;
int i = 1;
long pre = 0;
while (1) {
long value = i * 9 * pow(10, i - 1);
if (n > value) {
pre += value;
i++;
}
else {
break;
}
}
long remain = n - pre;
long y = remain / i;
long z = remain % i;
i--;
long start;
if (z > 0)
start = pow(10, i) + y;
else
start = pow(10, i) + y - 1;
stringstream ss;
ss << start;
string str;
ss >> str;
if (z > 0)
return (str[z - 1]) - 48;
else
return (str[str.length() - 1]) - 48;
}
};

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. devops model

    1,自动部署() 2,在线监控 (可视化,智能化) 3,故障诊断 (故障识别,故障隔离) 4,快速定位 (识别环境与程序,数据等问题) 评测工具开发---- 统计条目增加大项目: BI中权限―拨测数据 ...

  2. 小程序 修改按钮button样式:去边框、圆角及文字居左对齐、修改按钮高度

    因为有要button和view显示的样式相同的需要 所以要去掉按钮的边框,圆角,背景色,文字需要居左对齐,代码如下: 关键是按钮的样式: 1. 去掉边框: .user-phone-btn::after ...

  3. ubuntu 上已经安装libxml2还提示需要reinstall的解决方法

    最近在ubuntu上配置环境,遇到一些奇怪的问题,已经安装了libxml2了,运行 apt-get install libxml2提示已经是最新版本了,可以在安装软件的时候还是提示没有libxml2, ...

  4. CentOS中文乱码问题的解决方法

    一.CentOS系统访问 xxx.cn ,发现中文乱码于是用以前的方式:# yum -y install fonts-chinese # yum -y install fonts-ISO8859 Ce ...

  5. 剑指Offer——把二叉树打印成多行

    题目描述: 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 分析: 二叉树的层次遍历,利用队列. 代码: /* struct TreeNode { int val; struct T ...

  6. 荣誉墙项目day28 django常用函数

    1.在网页上渲染字符串from django.http import HttpResponsereturn HttpResponse(u"hello world") 2.渲染网页f ...

  7. LINux网络的NAPI机制详解一

    在查看NAPI机制的时候发现一篇介绍NAPI引入初衷的文章写的很好,通俗易懂,就想要分享下,重要的是博主还做了可以在他基础上任意修改,而并不用注明出处的声明,着实令我敬佩,不过还是附上原文链接! ht ...

  8. PCI 设备详解二

    上篇文章主要从硬件的角度分析了PCI设备的特性以及各种寄存器,那么本节就结合LInux源代码分析下内核中PCI设备的各种数据结构以及相互之间的联系和工作机制 2016-10-09 注:一下代码参考LI ...

  9. Java根据IP地址获取MAC地址

    先使用ping -n  2 10.0.0.1 命令,如果返回的结果中含有TTL字符,证明ping 10.0.0.1是能ping通的,即可达的.如果在Linux机器上请使用 ping -c 2 10.0 ...

  10. 使用Django和Python创建Json response

    版权声明:本文为博主原创文章,欢迎转载. https://blog.csdn.net/fengyu09/article/details/30785101 使用jquery的.post提交,并期望得到多 ...