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 ...
随机推荐
- PhpExcel中文帮助手册|PhpExcel使用方法
下面是总结的几个使用方法 include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include 'PHPExcel/ ...
- 使用NIFTI指令画nii图像
❤ 关于几种显示工具 mricro:显示出来的左右脑是反着的: mricroN,SPM,xjview,BrainNetViewer:显示出的左右脑是正确的,并且对于做过仿射变换的图像可以自动识别并且校 ...
- C#.NET 大型通用信息化系统集成快速开发平台 4.0 版本 - 多系统开发接口 - 苹果客户端开发接口
最近工作上需要,给苹果客户端开发接口,实现集中统一的用户管理,下面是接口调用参考. 1: 获取OpenId? http://127.0.0.1/GetOpenId.ashx?username=Admi ...
- 杭电1008 Elevator
#include <stdio.h> #include <stdlib.h> int main() { int n; int i,j; int num[101]; while( ...
- hadoop1.2.1伪分布模式配置
1.修改core-site.xml,配置hdfs <configuration> <property> <name>fs.default.name</name ...
- FineUI小技巧(2)将表单内全部字段禁用、只读、设置无效标识
需求描述 对表单内的所有字段进行操作也是常见需求,这些操作有: 禁用:表单字段变灰,不响应用户动作. 只读:表单字段不变灰,但不接受用户输入(实际上是设置DOM节点的readonly属性),有触发器的 ...
- SQL基础之select
1.认识select select的主要语法如下,这个很重要因为只有记住了整体的结构才能应对任何情况.从中可以看到select的强大主要就是建立在where.group by.having.order ...
- 我们来八一八阿里云OS的实质和历史
有个姓许的朋友在微信公众号上这样评论: 但是楼主对yunos的了解程度有多少,建议去了解下再评价别人,免费给你普及下:http://www.ithome.com/html/digi/109484.ht ...
- 基于DDD的.NET开发框架 - ABP缓存Caching实现
返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应 ...
- Pathoto项目:AWS+golang+beego搭建
帮兄弟写了一个网站,由于要在国外使用,选择了AWS作为服务器. 不知道后面的价格如何,12个月免费的确吸引了我. 花费3天时间,从注册到服务器搭建访问,终于搞定了.下面记录一下其中容易出错的命令. 1 ...