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的更多相关文章

  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. nginx服务器安装与启动

    nginx服务器介绍 1.nginx服务器功能 nginx服务器可以扮演:轻量级的web服务器.应用服务器.代理服务器.反向代理服务器.后台服务器.CDN缓存服务器 nginx的基本模块: 内核模块C ...

  2. wflag

    http://stackoverflow.com/questions/41312622/how-to-echo-an-alert-in-php-a-string-some-confusion-with ...

  3. JAVA分支语句例题

    1.输入年份判断是闰年还是平年? System.out.println("请输入年份:"); Scanner sc=new Scanner(System.in); String s ...

  4. DELPHI2007 安装ACTIVEX插件的方法

    先新建一个Package    file----NEW-----Package Delphi for win32, 再在Component->Import Component里面添加好Activ ...

  5. Java 判断图片资源的存在否

    question: 如题,举个例子吧 String image ="http://info-database.csdn.net/Upload/2010-10-30/735-60sap1030 ...

  6. What's Assembly - CSharp - Editor - first pass.dll? Best How to Fix Assembly - CSharp - Editor - first pass.dll Error Guide

    If you've found yourself here, I'm guessing that you're getting Assembly - CSharp - Editor - first p ...

  7. 数据传输:JSON,XML

    一.调用Ajax需要的JSON数据    1.url 处理页面    2.data 传递数据    3.datatype返回数据类型    4.type 提交数据方式    5.success成功之后 ...

  8. SQL代码创建表例子

    create table class ( code ) primary Key, name ) ); create table student ( code ) primary Key, name ) ...

  9. asr,tts,vsr

    http://max.book118.com/html/2014/0814/9432056.shtm   ASR技术的基础主要是信号处理和概率模型. 信号处理技术 语音信号处理 谱分析   基于时间的 ...

  10. A+Bproblem

    package A+Bproblem; /* * A+B Problem 时间限制:3000 ms  |  内存限制:65535 KB 难度:0 描述 此题为练手用题,请大家计算一下a+b的值 输入 ...