leetcode【400】Nth Digit(python)

版权声明:本文为博主原创文章,未经允许,不得转载,如需转载请注明出处 https://blog.csdn.net/ssjdoudou/article/details/83869532

写在最前面:

为什么我觉得这道简单的题好难啊...感觉自己是个数学渣渣

纯原创,不会有人和我一样奇葩的思路的...

leetcode【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 positive and will fit within the range of a 32-bit signed integer (n < 231).

Example 1:

Input:
3

Output:
3

Example 2:

扫描二维码关注公众号,回复: 4057730 查看本文章
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.

求出几位数

在这个几位数的第几个

属于哪个数字

属于哪一个数字的第几个字符

自己领悟吧~

class Solution:
    def findNthDigit(self, n):
        """
        :type n: int
        :rtype: int
        """
        digit = 1
        while n > digit*9*10**(digit-1):
            n -= digit*9*10**(digit-1)
            digit += 1
        a = n // digit
        b = n % digit
        if a == 0:
            c = 10 ** (digit-1) + a
        else:
            c = 10 ** (digit-1) + a -1
        if b == 0:
            return int(str(c)[digit-1])
        else:
            return int((str(c+1)[b-1]))

猜你喜欢

转载自blog.csdn.net/ssjdoudou/article/details/83869532