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.


思路:元素的区间长度分别为9,90,900,9000,以此类推,每个区间内元素长度分别为1,2,3,4等。
1。以数字为单位进行遍历,用sum表示总长度,每个数字遍历后加上该数字的长度,直至变量sum刚刚超过输入的整数n,然后往回退一个长度,即是n对应的数字,然后再根据n和sum之间的差值计算它是第几位。
2。上一种方法的效率很差。考虑到在每个区间里,每次自增的长度都一样,因此可以直接判断sum+下一个区间的总长度是否超过n。
如果没超过n,那么说明n不在这个区间里,直接sum加上下个区间的总长度然后继续判断,直至定位到n所在的区间,然后再计算该数字在区间里的位置,最后求解。
需要注意的地方:
用于保存区间元素长度的变量base需要用long格式,用int或者unsigned都会溢出。

class Solution {
public:
    int findNthDigit(int n) {
        ;
        unsigned stepLen = , num = ;
        unsigned ;
        ;
        )
            ;
        while (sum + base * stepLen <= n)    {
            sum += base * (stepLen++);
            num += base;
            ;
        }

        n -= sum;
        num += n / stepLen;
        ;i < stepLen - n % stepLen; i++)    {
            digit = num % ;
            num /= ;
        }
        return digit;
    }
};

Nth Digit | leetcode的更多相关文章

  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. C++版 - Leetcode 400. Nth Digit解题报告

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

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

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

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

  5. LeetCode——Nth Digit

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

  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]date命令时间戳和时间之间的转换

    非常多时候我们查看数据库的数据,或者是一些别人系统中的数据须要用时间戳来查询.或者查询出来的结果是个时间戳. 还有时候,查询条件须要输入时间戳. 我之前的办法就是用在线工具来完毕,后来用mac了.我觉 ...

  2. 容器大小的改变以及容器操作可能使迭代器失效、vector对象的容量变化

    1 改变容器的大小 我们可以使用resize来增加或缩小容器,与往常一样,array不支持resize.如果当前大小大于所要求的大小,容器后面的元素会被删除:如果当前大小小于新大小,会将新元素添加到容 ...

  3. java查询手机号码归属地

    package com; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRe ...

  4. Qt Quick 简单教程 - 1 (代码备忘)

    qmlscene 未安装 由于出现上面的情况,我开始转战Windows 下学习,昨天安装好了Qt Sdk了,哟吼吼吼. mail.qml内容: import QtQuick 2.3 import Qt ...

  5. String filePath = request.getSession().getServletContext().getRealPath("/");这句话返回的路径是什么,解释下getRealPath("/")函数中的"/"表示什么意思

    request.getSession().getServletContext() 获取的是Servlet容器对象,相当于tomcat容器了.getRealPath("/") 获取实 ...

  6. ASP.NET反射

    (转载至博客园 dodo-yufan) 两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏的生理情况.这是如何做到的呢?B超是B型超声波,它可以透过肚皮通过向你体 ...

  7. Android no such table (找不到表)

    今天在学习项目时,一直报错no such table Users.说找不到Users表.我就纳闷了,数据库是复制过去的,检查表名也没有写错.这是怎么回事呢?检查了半天才发现原来是数据库的路径错了. 我 ...

  8. 总结一下const和readonly

    const和readonly的值一旦初始化则都不再可以改写: const只能在声明时初始化:readonly既可以在声明时初始化也可以在构造器中初始化: const隐含static,不可以再写stat ...

  9. SQL Server 事务与锁

    了解事务和锁 事务:保持逻辑数据一致性与可恢复性,必不可少的利器. 锁:多用户访问同一数据库资源时,对访问的先后次序权限管理的一种机制,没有他事务或许将会一塌糊涂,不能保证数据的安全正确读写. 死锁: ...

  10. Oracle初始化

    /*第1步:创建临时表空间 */ create temporary tablespace mdb_temp tempfile 'G:\data\oracle\mdb_temp.ora' size 10 ...