算法27-----第N个数字

1、题目:

在无限的整数序列 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的一部分。

2、思路:

位数相加,N - 位数。

3、代码

    def findNthDigit(self, n):
        """
        :type n: int
        :rtype: int
        """
        num = len(str(n))
        a,b,c = 0,0,0
        for i in range(1,num+1): 
            b = a
            a += i*(10**i-10**(i-1))
            if a>=n:
                c = i
                break
        temp=0
        if num!=1:
            temp = 10**(c-1)-1
        # b = a - num *temp
        k = (n-b)//c
        l = (n-b) % c
        if l == 0:
            return int(str(k + temp)[-1])
        else:
            return int(str(k + temp+1)[l-1])
原文地址:https://www.cnblogs.com/Lee-yl/p/9591721.html