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. PHP函数addslashes和mysql_real_escape_string的区别

    转自:http://www.jb51.net/article/49205.htm   这篇文章主要介绍了PHP函数addslashes和mysql_real_escape_string的区别,以及一个 ...

  2. C#生成电子印章源码

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  3. 创建Java不可变类

    不可变(immutable)类的意思是创建该类的实例后,该实例的Field是不可改变的,Java提供的8个包装类和java.lang.String类都是不可变类. 如果需要创建自定义的不可变类,可遵守 ...

  4. WebStorm keyboard shortcuts

    ctrl + D 向下复制 下面是Webstorm的一些常用快捷键: shift + enter: 另起一行 ctrl + alt + L: 格式化代码 control + E:  光标跳到行尾 it ...

  5. session和token的区别

    session的使用方式是客户端cookie里存id,服务端session存用户数据,客户端访问服务端的时候,根据id找用户数据 而token一般翻译成令牌,一般是用于验证表明身份的数据或是别的口令数 ...

  6. 如何检测浏览器是否安装了Adblock,uBlock Origin,Adguard,uBlock等广告屏蔽插件

    由于我们网站上的广告经常被一些广告插件给屏蔽掉,上级给我下达了一个检测浏览器是否安装了屏蔽广告的插件的任务. 经过研究,借鉴,参考,整合了如下三种解决方案.   方案一: 利用广告插件通过对含有goo ...

  7. android上传图片、视频、文件,服务端使用wcf接收

    最近一直在搞android上传图片.视频.文件,服务端使用wcf接收,本文对调试中的遇到的问题进行记录. 首先android上传一些小图片是比较容易的一天下来差不多就能调试出来,但是上传一些大的文件时 ...

  8. XDU 1140 寻找万神(字符串匹配)

    学会strstr的使用 strstr(str1,str2)函数用于判断字符串str2是否是str1的子串.如果是,则该函数返回str2在str1中首次出现的地址:否则,返回NULL. #include ...

  9. vuejs模仿实现一个电影分享类网站

    前几天朋友发了一个电影分享类网站:http://dy.bingthink.top/ 感觉界面还挺好看的,所以打算用vuejs模仿写一个. 从后台转前端,要学习的东西挺多的,仅以此记录我的学习历程,闲的 ...

  10. Web前端页面的浏览器兼容性测试心得(二)搭建原汁原味的IE8测试环境

    如果你做的页面被老板或PM要求兼容IE8,你就值得同情了.IE8不支持HTML5,在2017年的前端界,开发者不涉及HTML5标准简直寸步难行.然而,有一个可怕的事实客观存在,那就是IE8是Win7系 ...