题目
难度:★★☆☆☆
类型:数组
在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 个数字。
注意:
n 是正数且在32为整形范围内 ( n < 231)。
示例
示例 1:
输入: 3
输出: 3
示例 2:
输入: 11
输出: 0
说明:
第11个数字在序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... 里是0,它是10的一部分。
解答
这道题目题面上很难理解,大家可以先不用在意这道题。
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('0')