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.

这道题目我之前见过。。。。一开始用下边注释的代码写,也通过了不过有点费时。后来改成取模。

class Solution {
public:
int findNthDigit(int n) {
// 2: 90 3:900 4: 9000 5:90000 6: 900000 7:9000000 8:90000000 9:900000000 10:9000000000
long x = ;
long t = ;
if (n < ) return n;
while (n > x * t) {
n -= x * t;
x++;
t *= ;
}
long sum = ;
int m = n % x;
if (m == ) return ((long)pow(10.0,x-) + n/x - )%;
else {
long q = (pow(10.0, x-) + (n/x) );
for (int j = ; j < x - m; ++j) q/=;
return q%;
}
/*
for (int i = 0; i < t; ++i) {
sum += x;
if (sum == n) {
return (i % 10);
}
else if (sum > n) {
long y = sum - n;
long z = pow(10.0,(x - 1)) + i;
cout <<z;
for (int j = 0; j < y; ++j) {
z /= 10;
}
return z%10;
}
}
*/
}
};

leetcode 400 Add to List 400. Nth Digit的更多相关文章

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

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

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

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

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

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

  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——Nth Digit

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

  7. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  8. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

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

随机推荐

  1. POJ 2267 From Dusk till Dawn or: Vladimir the Vampire(最短路变形)

    题意: 有一个吸血鬼要旅游, 他只能在晚上6点到第二天凌晨6点行动(18:00 ~ 6:00), 然后每天中午12点要喝1L的血(12:00), 现有m条火车的发车时间和行程时间, 问他从a到达b需要 ...

  2. ES6(Proxy 和 Reflect)

    Proxy 和 Reflect 1.Proxy 和 Reflect 的概念 Proxy 意为 ‘代理’,连接了用户和真实对象之间的一个层 Reflect 意为‘反射’   反射的是Object 2.适 ...

  3. Leetcode 306.累加数

    累加数 累加数是一个字符串,组成它的数字可以形成累加序列. 一个有效的累加序列必须至少包含 3 个数.除了最开始的两个数以外,字符串中的其他数都等于它之前两个数相加的和. 给定一个只包含数字 '0'- ...

  4. E-R图

    百度百科:E-R图 100多个数据库,一万多张表,能否使用一张E-R图来表示呢?它是可以的.数据设计依赖于企业的数据,而不是数据库的设计,对企业数据适当做归类,会直接导致数据设计,最终画出E-R图,数 ...

  5. Go切片的操作

    package main import "fmt" //切片的操作 func main() { //创建slice var s []int //zero value for sli ...

  6. [NOIP2003] 普及组

    乒乓球 模拟 /*By SilverN*/ #include<iostream> #include<algorithm> #include<cstring> #in ...

  7. 理解 mysql行锁和表锁

    在调用存储过程中,就会涉及到表锁,行锁这一概念:所谓区别:有索引的时候就是行锁,没有索引的时候就是表索. innodb 的行锁是在有索引的情况下,没有索引的表是锁定全表的. 表锁演示(无索引) Ses ...

  8. Jquery操作事件

    1.文档加载事件 2.DOM单击双击事件 3.DOM获得焦点,失去焦点问题 4.DOM鼠标移入,移出事件 <!DOCTYPE html> <html> <head> ...

  9. sublime text 3注册码

    —– BEGIN LICENSE —– Michael Barnes Single User License EA7E-821385 8A353C41 872A0D5C DF9B2950 AFF6F6 ...

  10. CheckStyle: 解决Unicode导致LineLength出错的问题

    在checkstyle.xml中,加上如下代码: <?xml version="1.0" encoding="UTF-8"?> <module ...