Skip to content

400 Nth Digit

400. Nth Digit

题目: https://leetcode.com/problems/nth-digit/

难度:

Easy

思路:

这道简单题我服, tag是math,找规律

1- 9 : 9 → 只占1位 9 
10 - 99: 90 → 两位 90 * 2
100 - 999: 900 → 三位 900 * 3
1000 - 9999: 9000 → 四位 9000 * 4

AC代码来之不易,是参考别人的,这里的for i in range(9), 其实无论range多少都可以吧

class Solution(object):
    def findNthDigit(self, n):
        """
        :type n: int
        :rtype: int
        """
        for i in range(9):
            d = 9 * 10 ** i
            if n <= d * (i+1): break
            n -= d *(i+1)
        n -= 1

        return int(str(10**i + n / (i+1))[n % (i+1)])

我们一直在努力

apachecn/AiLearning

【布客】中文翻译组