leetcode【400】Nth Digit

image description

leetcode【400】Nth Digit

写在最前面:

为什么我觉得这道简单的题好难啊...感觉自己是个数学渣渣 纯原创,不会有人和我一样奇葩的思路的...

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:

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]))
    ArithmeticJia         3         1316         Leetcode         123    

David Ramon

ArithmeticJia

www.guanacossj.com

Life is Short,You need Python

Related Posts

You may like these post too

image description

leetcode【763】Partition Labels(使用贪心算法划分字母区间)

##写在最前面 刷leetcode就像头脑风暴,也是各种算法的灵活运用。 其实介绍一点语法,介绍一个模块的调用相比刷leetcode,做算法轻松太多,用来刷积分,刷文章数再简单不过. 可总要做一点有挑战的事才有意义吧. by the way,我破邮的教室都装上空调了,果然评了双一

image description

leetcode【507】Perfect

##写在最前面:刷点简单的题水一水 leetcode【507】Perfect Number   We define the Perfect Number is a positive integer that is equal to the sum of all its positi

Comments on this post

3 comments

image description

ArithmeticJia

good!

image description

ArithmeticJia

+1

Leave a comment

it’s easy to post a comment

image description
image description
image description
image description
image description
image description
image description
image description
image description

Copyright © 2019.Company name All rights reserved.苏ICP备19007197号