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. 取消jQuery validate验证

    有时候当我们在编辑页面点保存后加上了validate错误验证后又想用表单提交的方式返回界面没有清除验证就返回不了 加上这句话就清除验证了      注意:remove()是删除了相关标签  我这需求是 ...

  2. IPAD之分割视图 SplitViewController

    转载自:http://www.w3cschool.cc/ios/att-ios-ui-splitview-htm.html 1 分割视图的使用 分割视图是 iPad 的特定视图控制器用于管理两个视图控 ...

  3. Windows内核之进程基本含义以及进程的创建

    进程 1 进程的含义: 1.1   一个是操作系统用来管理进程的内核对象. 内核对象也是系统用来存放关于进程的统计信息的地方. 1.2   还有一个是地址空间,它包括全部可运行模块或DL L 模块的代 ...

  4. [React Native] Complete the Notes view

    In this final React Native lesson of the series we will finalize the Notes view component and squash ...

  5. MySQL导入txt文件

    "Flufy","Harold","cat","f","1993-2-4" "claws& ...

  6. GoF的23个经典设计模式

    以文本和思维导图的方式简明扼要的介绍了GoF的23个经典设计模式,可当成学习设计模式的一个小手册,偶尔看一下,说不定会对大师的思想精髓有新的领悟. GoF(“四人帮”,又称Gang of Four,即 ...

  7. Android四大组件之服务-Service 原理和应用开发详解

    一.Android 服务简介 Service是android 系统中的四大组件之一(Activity.Service.BroadcastReceiver.ContentProvider),它跟Acti ...

  8. swift 定义类方法(type methed)

    swift   中声明结构体或者枚举的类型方法,需要在func前加上关键字 ststic  ,但是如果要定义一个类的类方法时,需要用关键字 class class SomeClass { class ...

  9. Android_Spinner_Listener

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

  10. Android_ViewPager

    view1源代码及xml资源文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...