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. 题目内容 写一个 ...
随机推荐
- java调用ws服务
1.找到ws服务地址,例如:http://www.webxml.com.cn/WebServices/MobileCodeWS.asmx 2.新建项目 3.进入命令行窗口,进入当前项目src目录下,然 ...
- ImageView控件有关问题
了解了一下ImageView控件,这个控件本身及其属性倒没有什么特别之处.只是在看<第一行代码>时,郭大神写到创建drawable-xhdpi文件有些问题,首先先说drawable和mip ...
- resure挽救笔记本系统和一些相关的操作记录
使用fedora23很久了, 但是感觉不是很流畅, 出现了一些不太稳定的体验, 所以想改到centos7. 因为centos7的很多东西 跟 fedora23 很相近了. 所以应该是无缝过渡 是选择3 ...
- 关于#ifdef #ifndef
https://www.cnblogs.com/agnily/p/5848768.html 1.先看#ifdef的用法: #define KEY1_PA0 #ifdef KEY1_PA0 ------ ...
- HIHOcoder 1449 后缀自动机三·重复旋律6
思路 显然endpos的大小就对应了对应子串的出现次数,所以快速求出endpos的大小,然后用它更新对应子串长度(minlen[i]~maxlen[i])的答案即可 endpos的大小可以拓扑排序求出 ...
- 【Python】图形界面
# [[图形界面]]'''Python支持多种图形界面的第三方库,包括TkwxWidgetsQtGTK但是Python自带的库是支持Tk的Tkinter,无需安装任何包,可直接使用.''' #[Tki ...
- node.js基本概念简单解释
1:什么是回调函数? 2:什么是同步异步 3:什么是I/O 4:什么是单线程/多线程 5:什么是阻塞/非阻塞 6:什么是事件 7:什么是事件驱动 8:什么是事件驱动的回调 9:什么是事件循环 解释: ...
- 宠物属性控制_pet
classIndex 职业索引 DmgAddPct 根据职业的法伤或攻强来计算宠物增加的物理伤害,增加的伤害值等于玩家法伤或攻强的百分比 SpAddPct 根据职业的法伤或攻强来计算宠物增加的法术伤害 ...
- centos7 彻底卸载PHP7
[root@xxx php-memcached]# rpm -qa | grep php php70w-common--.w7.x86_64 php70w-devel--.w7.x86_64 php7 ...
- label和span的区别
label标签主要用于绑定一个表单元素,当点击label标签的时候,被绑定的表单元素就会获得输入焦点. <div class="form-group col-lg-12"&g ...