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. Decoder with 3 Inputs and 2 3 = 8 Outputs

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION

  2. socket 中午吃的啥

    http://www.cnblogs.com/thinksasa/archive/2013/02/26/2934206.html

  3. CentOS + Nginx + PHP-FPM(FastCGI) 配置CodeIgniter

    nginx官方现在已经针对centos提供了repository,所以现在可以直接通过yum来安装啦,很方便. nginx官方安装教程:http://nginx.org/en/download.htm ...

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

  5. spring容器IOC创建对象<二>

    问题?spring是如何创建对象的?什么时候创建对象?有几种创建方式?测试对象是单例的还是多例的 ?对象的初始化和销毁? 下面的四大模块IOC的内容了!需要深刻理解 SpringIOC定义:把对象的创 ...

  6. ubuntu 64bit arm-linux-gcc: No such file or directory 解决

    通过下面这个解决 ubuntu 64bit  arm-linux-gcc: No such file or directory 安装 sudo apt-get install lsb-core解决 h ...

  7. SecureCRT SSH VMware Ubuntu

    Ubuntu 10.4 装完后网络OK. NAT模式下, 可以上网. 宿主机和客户机可以彼此ping通. 主要是检查SSH服务, 和防火墙是否关闭(UBUNTU 默认没有安装SSH, 默认启动了防火墙 ...

  8. membership 在web.config中配置信息

    <?xml version="1.0" encoding="utf-8"?><configuration> <configSect ...

  9. mysql参数,蛮全的

    网上有很多的文章教怎么配置MySQL服务器,但考虑到服务器硬件配置的不同,具体应用的差别,那些文章的做法只能作为初步设置参考,我们需要根据自己的情况进行配置优化,好的做法是MySQL服务器稳定运行了一 ...

  10. 常用yum命令

    yum list 查询所有可用软件包 yum search 关键字 查询和关键字相关的包 yum -y install 包名 加上-y自动回答yes yum -y update 包名 升级  yum ...