【LeetCode】400. Nth Digit 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/reach-a-number/description/
题目描述
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.
题目大意
找出一个连续的自然数序列中,第N位
数字是多少。
解题方法
我们要得到第N位数字,如果直接暴力是超时的。正确的做法是找规律:个位数字有9个,2位数字有910=90个,3位数字有9100=900个……所以我们先求出n是几位数字,然后判断第n个数字应该落在哪个自然数上,最后再求这个自然数会落在自然数的那一位上。
class Solution(object):
def findNthDigit(self, n):
"""
:type n: int
:rtype: int
"""
_len = 1
cnt = 9
start = 1
while n > _len * cnt:
n -= _len * cnt
_len += 1
cnt *= 10
start *= 10
start += (n - 1) / _len
return int(str(start)[(n - 1) % _len])
参考资料:http://www.cnblogs.com/grandyang/p/5891871.html
日期
2018 年 11 月 27 日 —— 最近的雾霾太可怕
【LeetCode】400. Nth Digit 解题报告(Python)的更多相关文章
- C++版 - Leetcode 400. Nth Digit解题报告
leetcode 400. Nth Digit 在线提交网址: https://leetcode.com/problems/nth-digit/ Total Accepted: 4356 Total ...
- [leetcode] 400. Nth Digit
https://leetcode.com/contest/5/problems/nth-digit/ 刚开始看不懂题意,后来才理解是这个序列连起来的,看一下第几位是几.然后就是数,1位数几个,2位数几 ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
随机推荐
- RNA-seq 生物学重复相关性验证
根据拿到的表达矩阵设为exprSet 1.用scale 进行标准化 数据中心化:数据集中的各个数字减去数据集的均值 数据标准化:中心化之后的数据在除以数据集的标准差. 在R中利用scale方法来对数据 ...
- Nginx在make时报错[objs/Makefile:469: objs/src/core/ngx_murmurhash.o] Error
Nginx在make时报错[objs/Makefile:469: objs/src/core/ngx_murmurhash.o] Error 在安装目录下执行 vim obj/Markfile 把 ...
- python-django-模板标签
注意:这个控制语句和python的差不多,但是记住必须有endfor 和endif 结尾 模板文件的django格式的注释是不会出现再网页渲染的源代码当中的 使用列子: <!DOCTYPE ht ...
- Web网页服务器软件——介绍
Web网页服务器软件与硬件服务器的关系,就像软件和电脑的关系. 目前有,世界使用排列第一名的Apache.还有可以在Linux系统下快速方便地搭建出LNMP Web服务环境的Nginx(其中LNMP分 ...
- 论文翻译:2020_Weighted speech distortion losses for neural-network-based real-time speech enhancement
论文地址:基于神经网络的实时语音增强的加权语音失真损失 论文代码:https://github.com/GuillaumeVW/NSNet 引用:Xia Y, Braun S, Reddy C K A ...
- keras房价预测数据集boston_housing.npz文件下载
github地址: https://github.com/yuxiwang66/boston_housing 码云地址: https://gitee.com/polarisslk/boston_hou ...
- accomplish, accord
accomplish =achieve; accomplishment=achievement. accomplished: well educated/trained, skilled. skill ...
- 集合类——Map集合、Properties属性文件操作
1.Map集合 Collection集合的特点是每次进行单个对象的保存,若要对一对对象来进行保存就只能用Map集合来保存.即Map集合中一次可以保存两个对象,且这两个对象的关系是key = value ...
- 通过Shell统计PV和UV
PV.UV是网站分析中最基础.最常见的指标.PV即PageView,网站浏览量,指页面的浏览次数,用以衡量网站用户访问的网页数量.用户没打开一个页面便记录1次PV,多次打开同一页面则浏览量累计:UV即 ...
- 启动Springboot 报错 Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Sat Jan 12 15:50:25 CST 2019 There was an unexpected error (type=Not
解决方案:http://www.cnblogs.com/michaelShao/p/6675186.html