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. [AngularJS] angular-formly: expressionProperties

    angular-formly provides a very simple API to dynamically change properties of your field (like disab ...

  2. 【转】C/C++求模求余运算符——2013-08-20

    http://blog.csdn.net/whealker/article/details/6203629 求模运算符(%),或称求余运算符,也就是数学上所谓的除法中的余数,%两侧均应为整数, |小| ...

  3. NIO学习:异步IO实例

    工作模式: 客户端代码: package demos.nio.socketChannel; import java.io.ByteArrayOutputStream; import java.io.I ...

  4. 一个苹果证书怎么多次使用(授权Mac开发)——导出p12文件

    为什么要导出.p12文件 当我们用大于三个mac设备开发应用时,想要申请新的证书,如果在我们的证书里,包含了3个发布证书,2个开发证书,可以发现再也申请不了开发证书和发布证书了(一般在我们的证书界面中 ...

  5. Unity3D 之武器系统冷却功能的实现方式

    先上方法 //如果Fire1按钮被按下(默认为ctrl),每0.5秒实例化一发子弹 public GameObject projectile; public float fireRate = 0.5F ...

  6. Service层和DTO层的作用

    Service层主要提供的几个作用:1.将业务逻辑层进行封装,对外提供业务服务调用.2.通过外观模式,屏蔽业务逻辑内部方法.3.降低业务逻辑层与UI层的依赖,业务逻辑接口或实现的变化不会影像UI层.4 ...

  7. VS 2013 编译和使用 Boost

    以 1.58.0 版本 boost 为例, 当前系统版本为 Windows 8.1 x64   1 编译boost  当前解压路径 "D:\Libraries\boost_1_58_0&qu ...

  8. 周末充电之WPF(一).初试牛刀

    追的剧已经赶上更新的速度了,突然觉得一下子就闲了.趁着这点时间,刚好学点 WPF .看到这边,好多人估计得感叹技术宅约等于单身狗,哈哈.好了,赶紧进入学习状态. 关注 WPF 或者说对它感兴趣其实多半 ...

  9. dp方程

    1.        资源问题1 -----机器分配问题 F[I,j]:=max(f[i-1,k]+w[i,j-k]) 2.        资源问题2 ------01背包问题   F[I,j]:=ma ...

  10. 插入排序之python实现源码

    def insert_sort(old): for i in range(1, len(old)): for j in range(i, 0, -1): if(old[j] < old[j-1] ...