解法一: 一个几乎纯数学的解法

numbers:   1,...,9, 10, ..., 99, 100, ... 999, 1000 ,..., 9999, ...

# of digits:   9     +  90*2   +  900*3 + 9000*4 + ...

利用这个公式可以很容易的求出来Nth digit出现在一个几位数上。假设出现在一个4位数上。那么我们应该从1000的第一个1开始往后数 n - (9 + 90*2 + 900*3)个digits。那么第n个digits应该出现在那个4位数上呢?可以用L17算出来这个数是1000后面的第几个自然数, e.g. 5。res为最后剩余的digits的个数,这个res<=4。那么我们应该找 '1005' 中的第res-1个digit。

 class Solution(object):
def findNthDigit(self, n):
"""
:type n: int
:rtype: int
"""
sum = 0
i = 1
while n > sum + i*9*10**(i-1):
sum += i*9*10**(i-1)
i += 1
start = 10**(i-1)
step = (n - sum - 1)//i
res = n - sum - step*i
return int(str(start + step)[res-1])

Leetcode 400. Nth digits的更多相关文章

  1. C++版 - Leetcode 400. Nth Digit解题报告

    leetcode 400. Nth Digit 在线提交网址: https://leetcode.com/problems/nth-digit/ Total Accepted: 4356 Total ...

  2. [leetcode] 400. Nth Digit

    https://leetcode.com/contest/5/problems/nth-digit/ 刚开始看不懂题意,后来才理解是这个序列连起来的,看一下第几位是几.然后就是数,1位数几个,2位数几 ...

  3. 【LeetCode】400. Nth Digit 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. leetcode 400 Add to List 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 ...

  5. 【leetcode❤python】 400. Nth Digit

    #-*- coding: UTF-8 -*- class Solution(object):    def findNthDigit(self, n):        ""&quo ...

  6. [LeetCode] Reconstruct Original Digits from English 从英文中重建数字

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  7. [LeetCode] Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  8. [LeetCode] Remove Nth Node From End of List 移除链表倒数第N个节点

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  9. LeetCode: Remove Nth Node From End of List 解题报告

    Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...

随机推荐

  1. Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...

  2. Putty颜色设置

    默认的Putty颜色和字体太不好看了,得自己设置: 字体:毫无疑问Consolas, 10-point:看起来非常清新自然 颜色: * Default Foreground: 255/255/255  ...

  3. 使用HttpWebRequest和HtmlAgilityPack抓取网页(拒绝乱码,拒绝正则表达式)

    废话不多说, 直接说需求. 公司的网站需要抓取其他网站的文章,但任务没到我这,同事搞了一下午没搞出来.由于刚刚到公司, 想证明下自己,就把活揽过来了.因为以前做过,觉得应该很简单,但当我开始做的时候, ...

  4. 使用spring boot和thrift、zookeeper建立微服务

    Spring cloud适应于云端服务,也适用于企业信息化SOA建设.spring boot也是restful微服务开发的利器.但对于内网服务,即服务与服务之间的调用,spring并没有去刻意封装,也 ...

  5. ALinq Dynamic 使用指南——代码的获取与编译

    1.下载代码 ALinq Dynamic 项目托管在 CodePlex 网站,你可以使用浏览器下载压缩包,或者通过 SVN 获取. 项目网址:http://esql.codeplex.com/ 压缩包 ...

  6. [NOIP摸你赛]Hzwer的陨石(带权并查集)

    题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...

  7. [转]搞ACM的你伤不起(转自Roba大神)

    劳资六年前开始搞ACM啊!!!!!!!!!! 从此踏上了尼玛不归路啊!!!!!!!!!!!! 谁特么跟劳资讲算法是程序设计的核心啊!!!!!! 尼玛除了面试题就没见过用算法的地方啊!!!!!! 谁再跟 ...

  8. 东大oj-1511: Caoshen like math

    Worfzyq likes Permutation problems.Caoshen and Mengjuju are expert at these problems . They have n c ...

  9. vijos P1009清帝之惑之康熙

    </pre>背景康熙是中国历史乃至世界历史中最伟大的帝王之一,清除螯拜,撤除三藩,统一台湾,平定准葛尔叛乱:与此同时,出众的他也被世界各国遣清使臣所折服.康熙是历史上少有的全人,不仅文武兼 ...

  10. Python 练习册

    01:将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果 [图像处理] 类似于图中效果: py 2.7代码: from PIL import Image, Im ...