Leetcode 400. Nth digits
解法一: 一个几乎纯数学的解法
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的更多相关文章
- C++版 - Leetcode 400. Nth Digit解题报告
leetcode 400. Nth Digit 在线提交网址: https://leetcode.com/problems/nth-digit/ Total Accepted: 4356 Total ...
- [leetcode] 400. Nth Digit
https://leetcode.com/contest/5/problems/nth-digit/ 刚开始看不懂题意,后来才理解是这个序列连起来的,看一下第几位是几.然后就是数,1位数几个,2位数几 ...
- 【LeetCode】400. Nth Digit 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- 【leetcode❤python】 400. Nth Digit
#-*- coding: UTF-8 -*- class Solution(object): def findNthDigit(self, n): ""&quo ...
- [LeetCode] Reconstruct Original Digits from English 从英文中重建数字
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...
- [LeetCode] Remove K Digits 去掉K位数字
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- [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 ...
- LeetCode: Remove Nth Node From End of List 解题报告
Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...
随机推荐
- 树莓派B+安装archlinux arm版
按Archlinux官网操作而来,如有疑问参照官网:http://archlinuxarm.org/platforms/armv6/raspberry-pi 以我自己安装过程举例,我的SD卡挂载在ub ...
- PAT 1003. 我要通过!(20) JAVA
参考http://blog.csdn.net/bin8632/article/details/50216297 答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于PAT的"答 ...
- js checkbox 选中判断
var isSelect = ""; isSelect = $("#tblImgList" + " input[type='checkbox']&qu ...
- MYSQL游标使用
CREATE PROCEDURE `sp_UpdateData`() BEGIN ); /*修改会员卡的几个金额*/ DECLARE done INT DEFAULT FALSE; DECLARE c ...
- Notes on Convolutional Neural Networks
这是Jake Bouvrie在2006年写的关于CNN的训练原理,虽然文献老了点,不过对理解经典CNN的训练过程还是很有帮助的.该作者是剑桥的研究认知科学的.翻译如有不对之处,还望告知,我好及时改正, ...
- opencv6.5-imgproc图像处理模块之轮廓
接opencv6.4-imgproc图像处理模块之直方图与模板 这部分的<opencv_tutorial>上都是直接上代码,没有原理部分的解释的. 十一.轮廓 1.图像中找轮廓 /// 转 ...
- 微软分布式云计算框架Orleans(2):容灾与集群(1)
在上一篇:微软分布式云计算框架Orleans(1):Hello World,我们大概了解了Orleans如何运用,当然上一篇的例子可以说是简单且无效的,因为用了Orleans不可能只写一个Hello ...
- python内置数据类型-字典和列表的排序 python BIT sort——dict and list
python中字典按键或键值排序(我转!) 一.字典排序 在程序中使用字典进行数据信息统计时,由于字典是无序的所以打印字典时内容也是无序的.因此,为了使统计得到的结果更方便查看需要进行排序. Py ...
- Bootstrap滚动监听
滚动监听(Scrollspy)插件,即自动更新导航插件,会根据滚动条的位置自动更新对应的导航目标.其基本的实现是随着您的滚动,基于滚动条的位置向导航栏添加 .active class. <!DO ...
- 收藏Javascript中常用的55个经典技巧
1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键 <table border oncontextmenu ...