【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 ...
随机推荐
- python-django使用ORM模型增删改查CRUD
from weibo.models import WeiboUser as User user_obj = User.objects.get(pk=1) user_obj.pk Out[4]: 1 u ...
- KEGG数据库整理示例
已知KEGG数据库中ko_map.tab文件,K-->ko: 目标文件:map-->K 代码示例: #! /usr/bin/perl -w use strict; my %seq; ope ...
- idea数据库报错java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
通过idea操作数据库,进行数据的增加,运行时报错java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 原因:没有导入mysql-connec ...
- nodejs-os模块
JavaScript 标准参考教程(alpha) 草稿二:Node.js os模块 GitHub TOP os模块 来自<JavaScript 标准参考教程(alpha)>,by 阮一峰 ...
- MediaPlayer详解
[1]MediaPlayer 详细使用细则 [2]MediaPlayer使用详解_为新手准备 [3]MediaPlayer 概览
- Shell学习(十)——du、df命令
一.du 命令 1.命令格式: du [选项][文件] 2.命令功能: 显示每个文件和目录的磁盘使用空间. 3.命令参数: -a或-all 显示目录中个别文件的大小. -b或-bytes 显示目录或文 ...
- 【Linux】【Services】【SaaS】Spinnaker
1. 简介 1.1. 说明: Spinnaker 是 Netflix 的开源项目,是一个持续交付平台,它定位于将产品快速且持续的部署到多种云平台上.Spinnaker 通过将发布和各个云平台解耦,来将 ...
- 01_ubantu国内软件源配置
查找自己版本对应的软件源 https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/ 以下为19.10版本清华大学的,个人100M的带宽,平均安装速度在600K ...
- idea集成开发工具快捷键大全
1 执行(run) alt+r 2 提示补全 (Class Name Completion) ...
- WPF 引用第三方库的控件在设计器加上设计时数据和属性
本文告诉大家如何在 VisualStudio 2022 的 XAML 设计器中,在设计时给第三方控件加上设计用的属性和数据的方法 此功能要求使用不低于 VisualStudio 2019 的 16.8 ...