Nth Digit | leetcode
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...
Note:
n is positive and will fit within the range of a 32-bit signed integer (n < 231).
Example 1:
Input: 3 Output: 3
Example 2:
Input: 11 Output: 0 Explanation: The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which is part of the number 10.
思路:元素的区间长度分别为9,90,900,9000,以此类推,每个区间内元素长度分别为1,2,3,4等。
1。以数字为单位进行遍历,用sum表示总长度,每个数字遍历后加上该数字的长度,直至变量sum刚刚超过输入的整数n,然后往回退一个长度,即是n对应的数字,然后再根据n和sum之间的差值计算它是第几位。
2。上一种方法的效率很差。考虑到在每个区间里,每次自增的长度都一样,因此可以直接判断sum+下一个区间的总长度是否超过n。
如果没超过n,那么说明n不在这个区间里,直接sum加上下个区间的总长度然后继续判断,直至定位到n所在的区间,然后再计算该数字在区间里的位置,最后求解。
需要注意的地方:
用于保存区间元素长度的变量base需要用long格式,用int或者unsigned都会溢出。
class Solution { public: int findNthDigit(int n) { ; unsigned stepLen = , num = ; unsigned ; ; ) ; while (sum + base * stepLen <= n) { sum += base * (stepLen++); num += base; ; } n -= sum; num += n / stepLen; ;i < stepLen - n % stepLen; i++) { digit = num % ; num /= ; } return digit; } };
Nth Digit | leetcode的更多相关文章
- [LeetCode] Nth Digit 第N位
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...
- C++版 - Leetcode 400. Nth Digit解题报告
leetcode 400. Nth Digit 在线提交网址: https://leetcode.com/problems/nth-digit/ Total Accepted: 4356 Total ...
- 【LeetCode】400. Nth Digit 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Leetcode: Nth Digit
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...
- LeetCode——Nth Digit
Question Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... ...
- 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 ...
- [Swift]LeetCode400. 第N个数字 | 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 ...
- hdu 1597 find the nth digit
find the nth digit Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- find the nth digit(二分查找)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1597 find the nth digit Time Limit: 1000/1000 MS (Jav ...
随机推荐
- iostat来对linux硬盘IO性能进行了解
http://www.php-oa.com/2009/02/03/iostat.html
- linux服务器上
命令行>mysql -uwin -pwin2009进入mysql command状态>use mindo时入mindo数据库>source 500sql.txt执行sql
- JDK 动态代理分析
Java的代理有两种:静态代理和动态代理,动态代理又分为 基于jdk的动态代理 和 基于cglib的动态代理 ,两者都是通过动态生成代理类的方法实现的,但是基于jdk的动态代理需要委托类实现接口,基于 ...
- 使用DrawerLayout实现QQ5.0侧拉菜单效果
在上一篇文章中,我们介绍了怎么使用DrawerLayout来实现一个简单的侧拉菜单(使用DrawerLayout实现侧拉菜单),也就是我们常说的抽屉效果,GitHub上类似效果的实现方式非常多,实现出 ...
- Ubuntu16.04/windows7修改本地hosts文件
1. 从github上下载最新的hosts文件:https://serve.netsh.org/pub/ipv4-hosts/ ubuntu16.04: 第二步:Ctrl+Alt+T 打开ubuntu ...
- dateTimePicker日期时间插件-----限定节假日调休的可选择性
需求:在项目中需要一款这样的日期插件,可以选择年月日,时分秒,对法定节假日不能选择,因法定节假日进行的调休可以选择: 现在使用的比较多的日期插件比如:Wdatepicker,jqueryUI的date ...
- 介绍SmartUpload很好的网站
附带链接:http://www.cnblogs.com/elleniou/archive/2012/09/24/2700583.html
- [ lucene高级 ] 研讨如何进行Lucene的分布式应用
http://www.cnblogs.com/huangfox/archive/2010/10/15/1852206.html Lucene是个高度优化的倒转索引搜索引擎.它将倒转的索引存储在定制的文 ...
- android studio环境搭建-笔记1
自己干了几年测试(功能性的),最近比较闲,就自己学习下android(以前也有所接触,但那是几年前的一点皮毛,都忘记了). 先搭建谷歌推出的android studio(以前用eclipse搭建总觉得 ...
- [转] c# 数据类型占用的字节数
http://www.cnblogs.com/laozuan/archive/2012/04/24/2467888.html