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. thinkphp的目录结构

    ├─ThinkPHP.php 框架入口文件 ├─Common 框架公共文件 ├─Conf 框架配置文件 ├─Extend 框架扩展目录 ├─Lang 核心语言包目录 ├─Lib 核心类库目录 │ ├─ ...

  2. javaScript 要点(十五)HTML DOM 导航

    通过 HTML DOM,能够使用节点关系在节点树中导航. 1.HTML DOM 节点列表 getElementsByTagName() 方法返回节点列表.节点列表是一个节点数组. 下面的代码选取文档中 ...

  3. JNI函数复杂对象传递

    主要操作内容,包括如下几个部分: 1.在Native层返回一个字符串 2.从Native层返回一个int型二维数组(int a[ ][ ]) 3.从Native层操作Java层的类: 读取/设置类属性 ...

  4. UITableViewcell autolayout下动态高度

    项目中最经常使用的一个UI就是UITableView了.iOS7.8进一步优化了复用机制,用起来相当爽.配合Autolayout,适配工作减轻了非常多. 曾经做适配工作都是在heightForRow里 ...

  5. Oracle用户及角色的权限管理[Oracle基础]

    1.查看全部用户:   select * from dba_users;   select * from all_users;   select * from user_users; 2.查看用户或角 ...

  6. RapidXml用法

    一.写xml 文件 #include <iostream> #include "rapidxml/rapidxml.hpp" #include "rapidx ...

  7. 学习笔记之Python

    http://baike.baidu.com/view/21087.htm?fr=aladdin#reference-[12]-21087-wrap Python 基础教程(http://www.w3 ...

  8. js 三元运算符以及|| 和 && 测试

    var  a = '0';var  b = a ? 'me':'hi'; console.log(b);//false 有: undefined , 0, '', false,null//true  ...

  9. java_jdbc_反射

    package cn.itcast.Reflect; import java.lang.reflect.Constructor; import java.lang.reflect.Field; imp ...

  10. 建索引让SQL飞起来

    今天帮助看了一个哥们的数据库,帮他抓了一下等待事件,刚好有一个sql在等待事件中,顺便看看 监控等待事件 select a.SID, a.EVENT, b.OSUSER, b.username, b. ...