400. 第N个数字

在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …中找到第 n 个数字。

注意:

n 是正数且在32为整形范围内 ( n < 231)。

示例 1:

输入:

3

输出:

3

示例 2:

输入:

11

输出:

0

说明:

第11个数字在序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, … 里是0,它是10的一部分。

PS:

思路是:位数 数字个数(即个位数字一共9个 两位数一共90个)

1 9

2 90

3 900

… …

给定一个n,先找所在的区间(如1-9,10-99…),然后求出它是所在的区间的第多少个数字,再算出是这个数字的第几位。找区间按照公式n=n-9*1-90*2-900*3...-9*10^(i-1)*i,

求出的i指它在i位数的区间内,n为该区间第n个数字,假设公式得到的n=4,i=2,那么说明是两位数,4/i=2,说明是第2个数字,即11,4%i=0,说明是第二个数字的最后一位,即1。

再假设共识得到n=25,i=3,说明是三位数,25/3=8,25%3=1,说明三位数中第8+1个数字,是这个数字的第1位,即是108中的1


class Solution {
public int findNthDigit(int n) {
if(n<10) return n;
int i;//记录结果所在数字的区间是几位数
for(i=1;n>9*i*Math.pow(10,i-1);++i){
n-=9*i*Math.pow(10,i-1);
}
int index=n/i;//某个区间第index个数字
if(index==0) index=1;//不会出现第0个数字,起码从第一个数字起
int bit=n%i;//第index个数的第bit位,接下来找出这个位数
int number=(int)Math.pow(10,i-1)+index-1; //number为一个具体的i位数
if(bit==0)//说明在这个数字的最后一位
return number%10;
else return getIndexBit(number+1,bit,i);//bit不为0 时,数字要再加1
}
private int getIndexBit(int number,int bit,int i){ //获取数字number的第bit位数
int a=(int)(number/Math.pow(10,i-bit));
return a%10;
}
}

Java实现 LeetCode 400 第N个数字的更多相关文章

  1. Java实现 LeetCode 747 至少是其他数字两倍的最大数(暴力)

    747. 至少是其他数字两倍的最大数 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 ...

  2. Java实现 LeetCode 738 单调递增的数字(暴力)

    738. 单调递增的数字 给定一个非负整数 N,找出小于或等于 N 的最大的整数,同时这个整数需要满足其各个位数上的数字是单调递增. (当且仅当每个相邻位数上的数字 x 和 y 满足 x <= ...

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

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

  4. LeetCode:至少是其他数字两倍的最大数【747】

    LeetCode:至少是其他数字两倍的最大数[747] 题目描述 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素 ...

  5. LeetCode数组中重复的数字

    LeetCode 数组中重复的数字 题目描述 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. ...

  6. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  7. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  8. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  9. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

随机推荐

  1. 第一行Kotlin系列(二)Intent隐式显式跳转及向下传值

    1.Intent显式跳转页面 val button5 = findViewById<Button>(R.id.mButton5) button5.setOnClickListener { ...

  2. (2)通信中为什么要进行AMC?

    AMC,Adaptive Modulation and Coding,自适应调制与编码. 通信信号的传输环境是变化不定的,信道环境时好时差.在这种情景下,我们不可能按照固定的MCS进行信号发送.假如信 ...

  3. activiti工作流入门学习

    工作流一般在OA系统用的比较多,当然,只要有流程审批的地方都会用到,activiti只是开源的工作流中比较流行的一个,还有其他的开源的工作流,这里学习activiti工作流:前面部分是关于activi ...

  4. 【Jmeter学习】【第一节】【Jmeter的安装】

    转载至https://www.cnblogs.com/qinlangsky/p/11941230.html 写的非常详细

  5. php-fpm搜索php.ini很奇怪的一个现象

    php-fpm 找不到 php.ini phpinfo 或者 php -i 上来看,搜索的目录都是/usr/local/php/etc下,但是事实上并没用去找这个目录 bin/php --ini 来看 ...

  6. node的stream

    stream在Unix系统中是个标准的概念. In computer programming, standard streams are preconnected input and output c ...

  7. protus中出现invalid internal memory size ==NULL

    点击8086芯片,更改internal memory size的大小为0x10000

  8. 最全面的Android Studio使用教程

    http://www.admin10000.com/document/5496.html

  9. hdu2093 考试排名(还需完善)

    下面代码是借鉴的.好多的知识点等着完善 #include <iostream> #include <string> #include <algorithm> usi ...

  10. SpringBoot踩坑日记

    1.Closing JPA EntityManagerFactory for persistence unit 'default'错误导致springboot启动后终止 --------------- ...