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. Storm-源码分析- hook (backtype.storm.hooks)

    task hook 在某些task事件发生时, 如果用户希望执行一些额外的逻辑, 就需要使用hook 当前定义如下事件, emit, cleanup, spoutAck-- 用户只需要开发实现ITas ...

  2. 设计模式之——Builder建造模式

    Builder模式又叫建造模式,是用于组装具有复杂结构的实例的模式. 示例程序是编写一个文档,并且写入到文件中,该文档具有以下结构,含有标题,字符串,一些条目. Builder抽象类,为建造模式的核心 ...

  3. C. Mail Stamps---cf29c(离散化,图)

    题目链接:http://codeforces.com/problemset/problem/29/C 题意就是有n(1e5)个点,找到一条能把所有的点都包含在内的路径,由于点的编号是 1e9 所以不得 ...

  4. D. Mike and Feet---cf548D(最值)

    题目链接:http://codeforces.com/problemset/problem/548/D 给你n个数,对于(1,n)长度,让你找到线段的最小值的最大值是多少 #include<io ...

  5. Ubutun使用记录——语系错误(转)

    add by zhj: 对原文有修改,原文是在创建用户时出现的问题,而我是在使用psql时出现的, 但问题是相同的. 原文:http://www.douban.com/note/362250557/ ...

  6. 安装python3 centos

    1.在新centos中安装python3的步骤https://www.cnblogs.com/lclq/archive/2016/06/27/5620196.html 2.安装python3过程中报错 ...

  7. Jmeter(六)文件上传和下载文件

    一.Jmeter上传文件 编写脚本:               首先添加一个线程组,然后在线程组里面添加一个http请求,因为是发送数据,所有是post请求,写好上传的地址,然后写好文件路径     ...

  8. SPOJ - COT Count on a tree

    地址:http://www.spoj.com/problems/COT/en/ 题目: COT - Count on a tree #tree You are given a tree with N  ...

  9. 获取Json字符串中某个key对应的value

    JSONObject jsonObj= JSONObject.fromObject(jsonStr); String value= jsonObj.getString(key);

  10. MVP架构学习

    MVP架构学习 M:数据层(数据库,文件,网络等...) V:UI层(Activity,Fragment,View以及子类,Adapter以及子类) P:中介,关联UI层和数据层,因为V和M是相互看不 ...