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. 阿里云ONS而微软Azure Service Bus体系结构和功能比较

    阿里云ONS而微软Azure Service bus体系结构和功能比较 版权所有所有,转载请注明出处http://blog.csdn.net/yangzhenping.谢谢! 阿里云的开放消息服务: ...

  2. iOS 中UI控件的各种对齐方式总结

    1.textAligment : 文字的水平方向的对齐方式 取值 NSTextAlignmentLeft      = 0,    // 左对齐 NSTextAlignmentCenter    = ...

  3. android106 C基本数据类型

    #JNI java native interface #c的基本数据类型 * int:32位,能表示的数字是2的32次方个 * 最高位用来表示符号位,那么还剩下31位可以表示数值,所以能表示的数字就是 ...

  4. 用Java对xml文档进行遍历,更新,创建,删除

    import java.io.File;import java.io.FileInputStream; import javax.imageio.stream.FileImageInputStream ...

  5. C#知识总结

    Control类属于Sytem的命名空间 表示控制台标准的输入输出和错误流提供用于从控制台读取单个字符或正行的方法还提供了很多写入的的方法, static 关键字是对方法的修饰 二 数据类型的分类 数 ...

  6. Android_Intent_passValueForResult

    当SecondActivity需要回传参数时:1. MainActivity启动SecondActivity时需要调用startActivityForResult() 并定义请求码2. SecondA ...

  7. char与varchar区别

    char:储存定长数据,长度不够,以空格填满.储存效率高. varchar: 变长数据,根据数据长度储存,节省空间,效率低.

  8. c语言,strcat(),字符串拼接

    #include<stdio.h> #include<string.h> int main() {  char destination[25];  char  *zhang=& ...

  9. jquery登录验证插件

    最近写了个登录验证的jquery插件,其中功能还不是很完善,需要进一步改进,先放出来看看先. /** * 功能描述:本模块实现普通网站登录验证,以后可以添加二维码验证,以及第三方帐号登录验证 * 关联 ...

  10. MyEclipse常见配置及调试

    常见配置 1.配置workspace ----- 建议不要采用含有空格和中文目录 所有代码保存workspace空间中2.新建工程时,设置工程需要jre环境MyEclipse提供多种内置layout ...