LeetCode--400--第N个数字
问题描述:
在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 个数字。
注意:
n 是正数且在32为整形范围内 ( n < 231)。
示例 1:
输入:
3 输出:
3
示例 2:
输入:
11 输出:
0 说明:
第11个数字在序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... 里是0,它是10的一部分。
方法(times out):
class Solution(object):
def findNthDigit(self, n):
"""
:type n: int
:rtype: int
"""
if n < 10:
return n
lis = [0,1,2,3,4,5,6,7,8,9] for i in range(10,n+1):
templist = []
while i != 0:
temp = i % 10
i = i // 10
templist.append(temp)
templist.reverse()
for i in templist:
lis.append(i)
return lis[n]
官方:
1-9 9 * 1 = 9个
10-99 90 * 2 = 180个
100-999 900 * 3 = 270个
设 digit代表几位数1,2,3,base代表每位数的个数9,90,900,ith代表该数的起始位置10,100,1000
设 n = 12
首先判断12在
class Solution(object):
def findNthDigit(self, n):
"""
:type n: int
:rtype: int
"""
digit = 1
base = 9
ith = 1
while n > digit * base:
n -= digit * base
ith += base
digit += 1
base = 10*base
return ord(str((n-1)//digit + ith)[(n-1)%digit]) - ord('')
LeetCode--400--第N个数字的更多相关文章
- Java实现 LeetCode 400 第N个数字
400. 第N个数字 在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, -中找到第 n 个数字. 注意: n 是正数且在32为整形范围内 ( n < 231 ...
- C++版 - Leetcode 400. Nth Digit解题报告
leetcode 400. Nth Digit 在线提交网址: https://leetcode.com/problems/nth-digit/ Total Accepted: 4356 Total ...
- LeetCode:至少是其他数字两倍的最大数【747】
LeetCode:至少是其他数字两倍的最大数[747] 题目描述 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素 ...
- LeetCode数组中重复的数字
LeetCode 数组中重复的数字 题目描述 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. ...
- [LeetCode] Lexicographical Numbers 字典顺序的数字
Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...
- [LeetCode] Missing Number 丢失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] Number of Digit One 数字1的个数
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- [LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)
1. 题目名称 Consecutive Numbers 2 .题目地址 https://leetcode.com/problems/consecutive-numbers/ 3. 题目内容 写一个 ...
随机推荐
- [内核驱动] 链表LIST_ENTRY的操作(转)
转载:https://www.cnblogs.com/forlina/archive/2011/08/11/2134610.html 转载:http://www.xuebuyuan.com/15443 ...
- javaweb三大框架和MVC设计模式
javaweb三大框架和MVC设计模式 转载,原文请见https://blog.csdn.net/sunpeng19960715/article/details/50890705 一.MVC设计模式 ...
- Java:The hierarchy of the type is inconsistent错误
错误 The type com.jiuqi.dna.ui.language.INLStringGroup cannot be resolved. It is indirectly referenced ...
- Django框架(八) Django之ORM数据库操作
创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是一对一的关系( ...
- 【做题】spoj4060 A game with probability——dp
赛前做题时忽然发现自己概率博弈类dp很弱,心好慌.(获胜概率或最优解期望) 于是就做了这道题,续了特别久. 一开始列dp式子的时候就花了很长时间,首先搞错了两次,然后忘记了根据上一轮dp值直接确定选什 ...
- .net core mvc 错误信息显示 ModelState.AddModelError
关于ModelState.AddModelError错误信息不在前端页面显示问题.经过一位高人指定终于知道了为什么,在次写着警示自己看文档一定要仔细.再次感谢这为兄弟 https://www.cnbl ...
- (转) K-Means聚类的Python实践
本文转自: http://python.jobbole.com/87343/ K-Means聚类的Python实践 2017/02/11 · 实践项目 · K-means, 机器学习 分享到:1 原文 ...
- JZ2440存储管理器--SDRAM
为了cpu访问外部设备,ARM提供一个存储管理器部件,提供访问外部设备的所需的信号(对SDRAM.网卡.nor等设备进行初始化,以便存储器管理器配合CPU进行与外设数据通讯). CPU通常读写一 ...
- org.springframework.jdbc.UncategorizedSQLException: Error attempting to get column 'alarmGroup' from result set. Cause: java.sql.SQLException: Error
异常展示: org.springframework.jdbc.UncategorizedSQLException: Error attempting to get column 'alarmGroup ...
- KNN——图像分类
内容参考自:https://zhuanlan.zhihu.com/p/20894041?refer=intelligentunit 用像素点的rgb值来判断图片的分类准确率并不高,但是作为一个练习kn ...