问题描述:

在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 个数字。

注意:
是正数且在32为整形范围内 ( n < 231)。

示例 1:

输入:
3 输出:
3

示例 2:

输入:
11 输出:
0 说明:
第11个数字在序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... 里是0,它是10的一部分。

方法(times out):

 class Solution(object):
def findNthDigit(self, n):
"""
:type n: int
:rtype: int
"""
if n < 10:
return n
lis = [0,1,2,3,4,5,6,7,8,9] for i in range(10,n+1):
templist = []
while i != 0:
temp = i % 10
i = i // 10
templist.append(temp)
templist.reverse()
for i in templist:
lis.append(i)
return lis[n]

官方:

  1-9         9 * 1 = 9个

  10-99     90 * 2 = 180个

  100-999  900 * 3 = 270个

设 digit代表几位数1,2,3,base代表每位数的个数9,90,900,ith代表该数的起始位置10,100,1000

设 n = 12

首先判断12在

 class Solution(object):
def findNthDigit(self, n):
"""
:type n: int
:rtype: int
"""
digit = 1
base = 9
ith = 1
while n > digit * base:
n -= digit * base
ith += base
digit += 1
base = 10*base
return ord(str((n-1)//digit + ith)[(n-1)%digit]) - ord('')

LeetCode--400--第N个数字的更多相关文章

  1. Java实现 LeetCode 400 第N个数字

    400. 第N个数字 在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, -中找到第 n 个数字. 注意: n 是正数且在32为整形范围内 ( n < 231 ...

  2. C++版 - Leetcode 400. Nth Digit解题报告

    leetcode 400. Nth Digit 在线提交网址: https://leetcode.com/problems/nth-digit/ Total Accepted: 4356 Total ...

  3. LeetCode:至少是其他数字两倍的最大数【747】

    LeetCode:至少是其他数字两倍的最大数[747] 题目描述 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素 ...

  4. LeetCode数组中重复的数字

    LeetCode 数组中重复的数字 题目描述 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. ...

  5. [LeetCode] Lexicographical Numbers 字典顺序的数字

    Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...

  6. [LeetCode] Missing Number 丢失的数字

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  7. [LeetCode] Number of Digit One 数字1的个数

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  8. [LeetCode] Consecutive Numbers 连续的数字

    Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...

  9. [LeetCode] Single Number 单独的数字

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

  10. [LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)

    1. 题目名称   Consecutive Numbers 2 .题目地址 https://leetcode.com/problems/consecutive-numbers/ 3. 题目内容 写一个 ...

随机推荐

  1. 【python016--序列】

    一.列表,元组和字符串的共同点 --都可以通过索引得到每一个元素 --默认索引值总是从0开始 --可以通过分片的方法得到一个范围内的元素的集合 --有很多共同的操作符(重复操作符,拼接操作符.成员关系 ...

  2. 【python001-IDLE】

    一.python的下载地址:http://www.python.org 二. python的注释:# 三.>>> print("i love python")i ...

  3. NetworkExtension

    一, 按照网上的方法: iOS 无法获取 WiFi 列表?一定是因为你不知道这个框架 提交了申请.两个小时候后苹果回应邮件.意思就是如果只是使用 App Proxy, Content Filter, ...

  4. ERROR! The server quit without updating PID file (/application/mysql-5.6.40/data/db01-51.pid).

    centos7.5 安装mysql数据库报错 问题: [root@db01-51 scripts]# /etc/init.d/mysqld start Starting MySQL.Logging t ...

  5. Cannot add foreign key constraint @ManyToMany @OneToMany

    最近在使用shiro做权限管理模块时,使用的时user(用户)-role(角色)-resource(资源)模式,其中user-role 是多对多,role-resource 也是多对多.但是在使用sp ...

  6. 使用vs code搭建C开发环境

    关于搭建vscode的开发环境来开发c网上已经有很多类似的贴子,但是几乎都是直接给出tasks.json和launch.json文件,并未说明这两个文件的作用以及如何配置.这里面向纯小白着重说明下(1 ...

  7. utf-8并不"兼容" gb2312, gb18030

    注意 utf-8 并不是 向下 兼容"gb2312 gb18030"等编码, 也并不是说, utf-8就是比 gb2312等高级的编码! 比如在terminal中, 你开始使用的 ...

  8. SpringBoot 注解调用Redis缓存

    注解代码: import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.l ...

  9. NOI 2011 阿狸的打字机(AC自动机+主席树)

    题意 https://loj.ac/problem/2444 思路 ​多串匹配,考虑 \(\text{AC}\) 自动机.模拟打字的过程,先建出一棵 \(\text{Trie}\) 树,把它变成自动机 ...

  10. Echarts 地图上显示数值

    Echarts 地图上展示数值,效果如下: 上代码:关键代码用红色 series: [ { //name: '香港18区人口密度', type: 'map', mapType: 'jiangsu', ...