Java实现 LeetCode 400 第N个数字
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个数字的更多相关文章
- Java实现 LeetCode 747 至少是其他数字两倍的最大数(暴力)
747. 至少是其他数字两倍的最大数 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 ...
- Java实现 LeetCode 738 单调递增的数字(暴力)
738. 单调递增的数字 给定一个非负整数 N,找出小于或等于 N 的最大的整数,同时这个整数需要满足其各个位数上的数字是单调递增. (当且仅当每个相邻位数上的数字 x 和 y 满足 x <= ...
- C++版 - Leetcode 400. Nth Digit解题报告
leetcode 400. Nth Digit 在线提交网址: https://leetcode.com/problems/nth-digit/ Total Accepted: 4356 Total ...
- LeetCode:至少是其他数字两倍的最大数【747】
LeetCode:至少是其他数字两倍的最大数[747] 题目描述 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素 ...
- LeetCode数组中重复的数字
LeetCode 数组中重复的数字 题目描述 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
随机推荐
- Qt 视频播放器
#include <phonon/VideoPlayer> #include <phonon/SeekSlider> #include <phonon/MediaObje ...
- Jmeter参数化四种方式
JMeter的三种参数化方式包括: 1.用户参数 2.函数助手 3.CSV Data Set Config/CSV数据配置文件 4.用户自定义变量 一.用户参数 位置:添加-前置处理器-用户参数 操作 ...
- react 动态渲染echarts折线图,鼠标放大缩小
//折线图组件import React,{Component} from 'react'; import ReactEcharts from 'echarts-for-react'; class Ec ...
- 线上Kafka突发rebalance异常,如何快速解决?
文章首发于[陈树义的博客],点击跳转到原文<线上Kafka突发rebalance异常,如何快速解决?> Kafka 是我们最常用的消息队列,它那几万.甚至几十万的处理速度让我们为之欣喜若狂 ...
- ql的python学习之路-day14
前言:本节主要学习时间模块time.datetime python中的几种时间表示:1)时间戳 2)格式化的字符串时间 3)struct_time元组格式的时间 time.datetime模块源码: ...
- 枚举:Enum-Int-String之间的转换与扩展
示例枚举: public enum Color { [Description("红色")] Red, [Description("绿色")] Green = 7 ...
- 对CSS3中的transform:Matrix()矩阵的一些理解
只要有CSS基础的人肯定都知道,我们可以通过transform中的translate,scale,rotate,skew这些方法来控制元素的平移,缩放,旋转,斜切,其实这些方法呢都是为了便于开发者使用 ...
- c++实现lower_bound和upper_bound
#include <bits/stdc++.h> using namespace std; int a[] = {0,1,3,3,5,6,7,8,9,20,21,21,21,30,41,4 ...
- python迭代器,生成器
1. 迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外,迭代器的一大 ...
- flask之Blueprint蓝图
flask_Blueprint.py ''' flask中的Blueprint蓝图: (1)新建模块,例如Bp1.py,Bp2.py,在模块中创建蓝图实例 (2)通过app.register_blue ...