【leetcode❤python】 400. Nth Digit

#-*- coding: UTF-8 -*-
class Solution(object):
    def findNthDigit(self, n):
        """
        :type n: int
        :rtype: int
        """
        res=n
        if n>9:
            index=1
            mul=9
            res=0
            while True:
                tag=n-mul*index
                
                if tag>0:
                    n=tag;res+=mul
                    mul*=10
                    index+=1
                else:index-=1;break
            
            print res,index,n
            div=n/(index+1)
            
            mod=n%(index+1)
            #应该是加1 而不是加mod
            if mod==0:
                res=str(res+div)[-1]
            else:
                res=str(res+div+1)[mod-1]
            return int(res)

        return res

sol=Solution()
print sol.findNthDigit(100000000)

posted @ 2016-11-13 19:21  火金队长  阅读(421)  评论(0编辑  收藏  举报