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 ...
随机推荐
- DEDECMS之五 单页
在网站开发中经常碰到关于我们.联系方式等简单的页面,那么在DEDECMS中如何实现? 一.效果 以上左侧导航的链接都是单页,右边为内容部分 二.单页的实现 创建频道封来实现 1.常规选项 2.高级选项 ...
- Codevs 1230 STL万岁。。 。
题目描述 Description 给出n个正整数,然后有m个询问,每个询问一个整数,询问该整数是否在n个正整数中出现过. 输入描述 Input Description 第一行两个整数 n 和m. 第二 ...
- MYSQL游标使用
CREATE PROCEDURE `sp_UpdateData`() BEGIN ); /*修改会员卡的几个金额*/ DECLARE done INT DEFAULT FALSE; DECLARE c ...
- Cordova - 使用Cordova开发iOS应用实战3(添加Cordova控制台插件)
Cordova - 使用Cordova开发iOS应用实战3(添加Cordova控制台插件) 前文介绍了通过 Safari 的 Web检查器,可以看到控制台输出的信息.但有时这样调试代码不太方便,如果在 ...
- jboss的时区问题
默认情况下,jboss启动时,使用的时区是“+0:00”区,而中国所在的时间为"+8:00"区(所谓的东8区),最终java取当前时间时,总比北京时间慢8个小时 解决办法: 新建一 ...
- rotate 3d基础
基础 看了岑安大大的教程学习了3d基础,之前写了篇总结,觉得写的太散废话太多,重写一篇. 本文需要实现的效果如下:3d球 岑安的两篇教程写的很棒,但我感觉改变下顺序或许会更好理解. 我们把画布(此文所 ...
- 记一次ASP.NET网站的入侵和如何避免被入侵
ASP.NET网站入侵第二波(LeaRun.信息化快速开发框架 已被笔者拿下) 详细介绍请看第二波 首先我要申明的是不是什么语言写出来的程序就不安全,而是得看写代码的人如何去写这个程序 前些日子我去客 ...
- 中间件(middlebox)
Middleboxes (also known as network functions) are systems that perform sophisticated and often state ...
- Competition-based User Expertise Score Estimation-20160520
1.Information publication:sigir 2011 author:Jing Liu Harbin Institute of TechnologyMicrosoft Researc ...
- AVL树插入操作实现
为了提高二插排序树的性能,规定树中的每个节点的左子树和右子树高度差的绝对值不能大于1.为了满足上面的要求需要在插入完成后对树进行调整.下面介绍各个调整方式. 右单旋转 如下图所示,节点A的平衡因子(左 ...