Python O(1) no for loop


  • 0
    import math
    class Solution(object):
        def findNthDigit(self, n):
            def _calc_partition(m):
                return int(str(m-2) + max(0, (m-3))*'8' + ('90' if m > 2 else '10'))
            if n < 10: return n
            m = int(math.ceil(math.log(n, 10)))
            partition = _calc_partition(m)
            if partition > n:
                m -= 1
                partition = _calc_partition(m)
            a, b = divmod(n-partition, m)
            return (10**(m-1) + a)/(10**(m-b-1))%10
    

  • 0

    Idea is similar with others, though.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.