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. 什么是Cocos2d-x

    以下是官方对Cocos2d-x的说明."       Cocos2d-x is an open-source mobile 2D game framework, released under ...

  2. Git 安装与简单使用(新手必看)

    1.安装git,默认下一步下一步等待安装完成 2.设置全局账号 安装之后去快速启动栏点击GitBash git config --global user.name "xiefeng" ...

  3. modelsim remote

    远程桌面登陆我的台式机上的账号,然后运行modelsim 出现该问题: Unable to checkout a viewer license necessary for use of the Mod ...

  4. SQL语句修改表

    -- 更改字段类型 默认值 alter table 表名 alter column 字段名 类型 ALTER TABLE 表名 add DEFAULT ('修改后的默认值') for 字段名 WITH ...

  5. Unity3D 之UGUI 图片

    这里来降价下Unity3Dl的图片 先创建一个图片 图片的属性 Preserve Aspect -->保持图片的原始宽高比例 Set native Size -->图片原始尺寸 Image ...

  6. 关于.NET前后台提示框的那点事

    前言 关于提示框,或多或少都用到过,提示框常见方式两种:js原生alert() 和 div模拟弹层:下面以一个常见的需求业务场景来展现提示框的那点事: 正文内容 客户:需求方: 小白:实现方(全权负责 ...

  7. Android中log4j的运用

    网上一查关于android上面运用Log4j的运用,各种说需要添加多样的包的,照着log4j的官网教程看了下,给了个简单的输出到console上面的代码,似乎没什么用.看网上关于Log4j更多是在ja ...

  8. C#对于sql server数据库的简单操作

    1.在用windows模式登陆sql server 数据库 简历一个student的数据库,然后新建查询: create table student ( id int auto_increment p ...

  9. 学习笔记_Java get和post区别(转载_GET一般用于获取/查询资源信息,而POST一般用于更新资源信息)

    转载自:[hyddd(http://www.cnblogs.com/hyddd/)] 总结一下,      Get是向服务器发索取数据的一种请求      而Post是向服务器提交数据的一种请求,在F ...

  10. ORACLE 11g R2数据库安装硬件环境要求

    物理内存要求:最小1G,在windows7,windows8,windows8.1上最小2G. 虚拟内存(或分页空间)容量要求: Available RAM Swap Space Required B ...