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 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.
1-9 : count:9 * len:1
10-99: count:90 * len:2
100-999: count:900 * len:3
1000-9999: count: 9000 * len:4
maintain a count, len, start
- public class Solution {
- public int findNthDigit(int n) {
- int start = 1;
- int len = 1;
- long count = 9;
- while (n > len*count) {
- n -= len*count;
- start *= 10;
- len ++;
- count *= 10;
- }
- start += (n-1)/len;
- char res = Integer.toString(start).charAt((n-1)%len);
- return Character.getNumericValue(res);
- }
- }
Leetcode: Nth Digit的更多相关文章
- [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 ...
- LeetCode——Nth Digit
Question Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... ...
- 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 ...
- 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 i ...
- 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 ...
随机推荐
- Bit-Value Type
https://dev.mysql.com/doc/refman/5.7/en/bit-type.html MySQL 5.7 Reference Manual / ... / Bit-Val ...
- [转自Kevins的天空 http://rootsec.cn]rad studio 2007 up3方法
rad studio 2007 网络下载点: http://bbs.hnhyxy.com/bcb/CodeGear.RAD.Studio.2007.rar http://andy.jgknet.de/ ...
- X5学习笔记—给单元格添加颜色
设置grid某一个单元格的颜色 可以用dhtmlxgrid的原生态方法 setCellTextStyle (row_id, ind, styleString) 参数: rowid:行id cellin ...
- Linq&Lumbda
var y = from model in list.Where(s=>s.product==product||product=="") ...
- zepto源码--$.map,$.each,$.grep--学习笔记
从相对比较简单的说起: 1.$.grep 获取一个新数组,新数组只包含回调函数中返回 true 的数组项. 调用javascript中数组原生函数filter,对elements进行过滤,保留回调函 ...
- Linux CentOS 编绎安装Python 3.5
Linux CentOS 编绎安装Python 3.5 先决条件(若无安装,则不能编绎使用idle3):yum install tk-devel xz -d Python-3.5.0.tar.xzta ...
- ArcGIS Engine开发的ArcGIS 版本管理的功能
原文:ArcGIS Engine开发的ArcGIS 版本管理的功能 转自:http://blog.csdn.net/linghe301/article/details/7965901 这是以前的Arc ...
- SQL Server 未保存.sql文件,还想查看、修改一些建表语句、存储过程等怎么办?
SP_HELPTEXT 表名/视图名/存储过程名:
- 有了JSON.stringify(),处理json将变得更简单!!
之前处理json 需要拼接json字符串,但是,如果用上JSON.stringify()的话,忘了json语法以没关系了..... @{ ViewBag.Title = "GetStr&qu ...
- iOS 获取UUID
-(NSString*)GetUUID { CFUUIDRef puuid = CFUUIDCreate( nil ); CFStringRef uuidString = CFUUIDCreateSt ...