1. Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...
  2.  
  3. Note:
  4. n is positive and will fit within the range of a 32-bit signed integer (n < 231).
  5.  
  6. Example 1:
  7.  
  8. Input:
  9. 3
  10.  
  11. Output:
  12. 3
  13. Example 2:
  14.  
  15. Input:
  16. 11
  17.  
  18. Output:
  19. 0
  20.  
  21. Explanation:
  22. 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

  1. public class Solution {
  2. public int findNthDigit(int n) {
  3. int start = 1;
  4. int len = 1;
  5. long count = 9;
  6. while (n > len*count) {
  7. n -= len*count;
  8. start *= 10;
  9. len ++;
  10. count *= 10;
  11. }
  12. start += (n-1)/len;
  13. char res = Integer.toString(start).charAt((n-1)%len);
  14. return Character.getNumericValue(res);
  15. }
  16. }

Leetcode: Nth Digit的更多相关文章

  1. [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 ...

  2. LeetCode——Nth Digit

    Question Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... ...

  3. C++版 - Leetcode 400. Nth Digit解题报告

    leetcode 400. Nth Digit 在线提交网址: https://leetcode.com/problems/nth-digit/ Total Accepted: 4356 Total ...

  4. 【LeetCode】400. Nth Digit 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 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 ...

  6. 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 ...

  7. [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 ...

  8. hdu 1597 find the nth digit

    find the nth digit Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. find the nth digit(二分查找)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1597 find the nth digit Time Limit: 1000/1000 MS (Jav ...

随机推荐

  1. Bit-Value Type

    https://dev.mysql.com/doc/refman/5.7/en/bit-type.html MySQL 5.7 Reference Manual  /  ...  /  Bit-Val ...

  2. [转自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/ ...

  3. X5学习笔记—给单元格添加颜色

    设置grid某一个单元格的颜色 可以用dhtmlxgrid的原生态方法 setCellTextStyle (row_id, ind, styleString) 参数: rowid:行id cellin ...

  4. Linq&Lumbda

    var y = from model in list.Where(s=>s.product==product||product=="")                    ...

  5. zepto源码--$.map,$.each,$.grep--学习笔记

    从相对比较简单的说起: 1.$.grep  获取一个新数组,新数组只包含回调函数中返回 true 的数组项. 调用javascript中数组原生函数filter,对elements进行过滤,保留回调函 ...

  6. Linux CentOS 编绎安装Python 3.5

    Linux CentOS 编绎安装Python 3.5 先决条件(若无安装,则不能编绎使用idle3):yum install tk-devel xz -d Python-3.5.0.tar.xzta ...

  7. ArcGIS Engine开发的ArcGIS 版本管理的功能

    原文:ArcGIS Engine开发的ArcGIS 版本管理的功能 转自:http://blog.csdn.net/linghe301/article/details/7965901 这是以前的Arc ...

  8. SQL Server 未保存.sql文件,还想查看、修改一些建表语句、存储过程等怎么办?

    SP_HELPTEXT 表名/视图名/存储过程名:

  9. 有了JSON.stringify(),处理json将变得更简单!!

    之前处理json 需要拼接json字符串,但是,如果用上JSON.stringify()的话,忘了json语法以没关系了..... @{ ViewBag.Title = "GetStr&qu ...

  10. iOS 获取UUID

    -(NSString*)GetUUID { CFUUIDRef puuid = CFUUIDCreate( nil ); CFStringRef uuidString = CFUUIDCreateSt ...