https://leetcode.com/contest/5/problems/nth-digit/

刚开始看不懂题意,后来才理解是这个序列连起来的,看一下第几位是几。然后就是数,1位数几个,2位数几个,3位数几个,int范围2e10,所以处理到11位数差不多,仔细算一下可以更少,然后先找到是几位数,然后除以位数,找到这个数是多少,取余看是第几位,然后就可以了。我预处理每位的个数和开始的位置。

 class Solution {
public:
long long d[];
int b[];
int init(int n) {
d[] = ;
d[] = ;
b[] = ;
for (int i = ; i < ; i++) {
d[i] = d[i - ] * ;
b[i] = b[i - ] * ;
}
for (int i = ; i < ; i++) {
d[i] = d[i] * i;
// cout << d[i] << endl;
}
int i = ;
for (i = ; i < ; i++) {
if(d[i] < n)
n -= d[i];
else break;
}
n--;
int t1 = n / i, t2 = n % i;
int k = b[i] + t1;
stringstream ss; string s1;
ss << k; s1 = ss.str();
//cout << t1 << " asd " << t2 << endl;
//cout << s1 << endl;
//reverse(s1.begin(), s1.end());
return s1[t2] - ''; }
int findNthDigit(int n) {
return init(n);
}
};

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

  4. Leetcode 400. Nth digits

    解法一: 一个几乎纯数学的解法 numbers:   1,...,9, 10, ..., 99, 100, ... 999, 1000 ,..., 9999, ... # of digits:   9 ...

  5. 【leetcode❤python】 400. Nth Digit

    #-*- coding: UTF-8 -*- class Solution(object):    def findNthDigit(self, n):        ""&quo ...

  6. 400. Nth Digit

    这个EASY难度的题怎么感觉比H的还难,昨天没做出来,今天才做出来.. 呃啊..我生 气 啦.. 直接看的答案,耻辱. 1 digit: 1~9 总共1×9个 2 digits: 10~99 总共2× ...

  7. 400 Nth Digit 第N个数字

    在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 个数字.注意:n 是正数且在32为整形范围内 ( n < 231).示例 1:输入:3 ...

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

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

随机推荐

  1. sql STUFF用法

    sql STUFF用法 1.作用 删除指定长度的字符,并在指定的起点处插入另一组字符. 2.语法 STUFF ( character_expression , start , length ,char ...

  2. PSObject

    PSBASE the raw view of the object PSADAPTED the fully adapted view of the object PSEXTENDED just the ...

  3. MyEclipse与Mysql数据库的连接

    1.载入MySql驱动程序 Class.forName("com.mysql.jdbc.Driver");    // 载入MySql驱动程序 2.建立Connection连接对象 ...

  4. #define和预编译指令

    今天再总结一点#define和预处理指令的使用. 预处理过程扫描源代码,对其进行初步的转换,产生新的源代码提供给编译器.可见预处理过程先于编译器对源代码进行处理. 预处理指令是以#开头的代码行,#后是 ...

  5. android学习日记08--Paint画笔

    Paint 要绘图,首先得调整画笔,待画笔调整好之后,再将图像绘制到画布Canvas上,这样才可以显示在手机屏幕上.Android 中的画笔是 Paint类,Paint 中包含了很多方法对其属性进行设 ...

  6. 学习笔记之JAVA多线程

    Java程序设计实用教程 by 朱战立 & 沈伟 孙鑫Java无难事 Java 多线程与并发编程专题(http://www.ibm.com/developerworks/cn/java/j-c ...

  7. Activity的启动模式及回退栈的概念

    Activity的启动模式 standard 正常模式 在创建一个新的activity的时候,直接在栈顶创建一个新的activity singleTop 顶部单个 在创建一个新的activity的时候 ...

  8. Android_Intent_passValue(4)

    xml布局文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...

  9. C++的三大特性之一继承

    一.继承的相关基本概念 1.继承的定义     在C++中,可以使用继承来使新类得到已定义的一些类中的特性,这就好比与孩子从父亲母亲得到遗传类似,所以我们称原有的类为基类或父类,用原有类来生成新的类的 ...

  10. 我终于理解了LISP『代码即数据|数据即代码』的含义

    以前我一直不能理解LISP里引用的作用,感觉引用和字符串没什么区别.比如:> (define (func)     'ok) > (func) 'ok 这里把引用ok当做了函数func的返 ...