(LeetCode)Nth Digit --- 第几位数字

来源:互联网 发布:java 敏捷开发框架 编辑:程序博客网 时间:2024/04/30 22:39

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:3Output:3

Example 2:

Input:11Output:0Explanation: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.

Subscribe to see which companies asked this question

解题分析:

此题目的意思就是将所有的数字打开,从第一个往后数,看第几位是哪个数字?

基本的思路是一位时当0 ~ 9时候,共十个数,

                   两位时,10 ~ 99 共有 90个

                  三位时,。。。。。。。。。。。


我们可以计算范围,然后一次去掉已经计算出来的位数,然后算出,数字。

# -*- coding:utf-8 -*-__author__ = 'jiuzhang'class Solution(object):    def findNthDigit(self, n):        if n < 0:            return 0        count = 9        start = 1        length = 1        while n > (length * count):            n -= length * count            length += 1            start *= 10            count *= 10        start += (n - 1)/length        return int((str(start)[(n - 1)%length]))



0 0