class Solution {
public int findNthDigit(int n) {
int d = 1, count = 9;
while (n > (long) d * count) {
n -= d * count;
d++;
count *= 10;
}
int index = n - 1;
int start = (int) Math.pow(10, d - 1);
int num = start + index / d;
int digitIndex = index % d;
int digit = (num / (int)(Math.pow(10, d - digitIndex - 1))) % 10;
return digit;
}
}
def findNthDigit(n):
i = 1
char_num = 9 * pow(10,i-1) * i
# 首先是找到i
while n > char_num:
n -= char_num
i += 1
char_num = 9 * pow(10, i - 1) * i
# 找到在哪个数里
num = str(pow(10,i-1) + (n - 1) // i)
# 找到是这个数的第几个数字(从0开始)
index = (n-1) % i
return int(num[index])
class Solution {
public int findNthDigit(int n) {
// 求目标数字的长度
// 对每个长度的数位和求和
// 第一个大于等于n的长度就是所求
int len = 1;
long k = 1, sum = 9;
while (sum < n) {
// 长度+1
len++;
// 该长度蕴含的数字个数
k *= 10;
// 这些数字的数位和
sum += 9 * k * len;
}
// 上个len的数位和,求出当前len的第一个数从序列的哪一位开始
long last = sum - 9 * k * len;
// 求出是当前长度的第几个数
long idx = (n - last - 1) / len;
// 求出是当前数字的第几位
long pos = (n - last - 1) % len;
// 求出当前数字
long cur = k + idx;
// 求出那一位
for (int i = 0; i < len - pos - 1; i++) cur /= 10;
return (int) (cur % 10);
}
}
func findNthDigit(n int) int {
//d : 位数 count:当前位数下的所有整数的位数之和
d := 1
for count := 9; n > d*count; count *= 10 {
n -= d * count
d++
}
index := n - 1
start := int(math.Pow10(d - 1))
num := start + index/d
digitIndex := index % d
return num / int(math.Pow10(d-digitIndex-1)) % 10
}
class Solution {
public int findNthDigit(int n) {
//总结规律发现 每个区间段的数字有 9*bit 字符有 9*bit*i i表示这个数字是几位数 bit表示数量级
if(n == 0){
return 0;
}
long bit = 1;
int i = 1;
long count = 9;
<pre><code> //判断在那个区间
while(count < n){
n = (int) (n - count);
bit = bit * 10;
i++;
count = bit * i * 9;
}
//判断是那个数
long num = bit + (n - 1) / i;
//取余判断是那个数中的那个字符
int index = (n -1 ) % i + 1;
int res = (int) (num / Math.pow(10, i - index)) % 10;
return res;
}
</code></pre>
}
“`java
class Solution {
public int findNthDigit(int n) {
//总结规律发现 每个区间段的数字有 9*bit 字符有 9*bit*i i表示这个数字是几位数 bit表示数量级
if(n == 0){
return 0;
}
long bit = 1;
int i = 1;
long count = 9;
//判断在那个区间
while(count < n){
n = (int) (n - count);
bit = bit * 10;
i++;
count = bit * i * 9;
}
//判断是那个数
long num = bit + (n - 1) / i;
//取余判断是那个数中的那个字符
int index = (n -1 ) % i + 1;
int res = (int) (num / Math.pow(10, i - index)) % 10;
return res;
}
“`java
class Solution {
public int findNthDigit(int n) {
//总结规律发现 每个区间段的数字有 9*bit 字符有 9*bit*i i表示这个数字是几位数 bit表示数量级
if(n == 0){
return 0;
}
long bit = 1;
int i = 1;
long count = 9;
//判断在那个区间
while(count < n){
n = (int) (n - count);
bit = bit * 10;
i++;
count = bit * i * 9;
}
//判断是那个数
long num = bit + (n - 1) / i;
//取余判断是那个数中的那个字符
int index = (n -1 ) % i + 1;
int res = (int) (num / Math.pow(10, i - index)) % 10;
return res;
}
“`java
class Solution {
public int findNthDigit(int n) {
//总结规律发现 每个区间段的数字有 9*bit 字符有 9*bit*i i表示这个数字是几位数 bit表示数量级
if(n == 0){
return 0;
}
long bit = 1;
int i = 1;
long count = 9;
//判断在那个区间
while(count < n){
n = (int) (n - count);
bit = bit * 10;
i++;
count = bit * i * 9;
}
//判断是那个数
long num = bit + (n - 1) / i;
//取余判断是那个数中的那个字符
int index = (n -1 ) % i + 1;
int res = (int) (num / Math.pow(10, i - index)) % 10;
return res;
}
“`java
class Solution {
public int findNthDigit(int n) {
//总结规律发现 每个区间段的数字有 9*bit 字符有 9*bit*i i表示这个数字是几位数 bit表示数量级
if(n == 0){
return 0;
}
long bit = 1;
int i = 1;
long count = 9;
//判断在那个区间
while(count < n){
n = (int) (n - count);
bit = bit * 10;
i++;
count = bit * i * 9;
}
//判断是那个数
long num = bit + (n - 1) / i;
//取余判断是那个数中的那个字符
int index = (n -1 ) % i + 1;
int res = (int) (num / Math.pow(10, i - index)) % 10;
return res;
}
class Solution {
public int findNthDigit(int n) {
if (n == 0){
return 0;
}
long bit = 1;
int i = 1;
long count = 9;
while (count < n){
n = (int) (n - count);
bit = bit * 10;
i++;
count = bit * i *9;
}
//确定区间
long num = bit + (n-1) / i;
int index = (n - 1) % i + 1;
int res = (int)(num / Math.pow(10,i-index)) % 10;
return res;
}
}思路不清晰
class Solution {
public int findNthDigit(int n) {
if(n == 0) return 0;
long bit = 1;
long count = 9;
int i = 1;
while(n > count){
n = (int)(n - count);
bit *= 10;
i++;
count = 9 * bit * i;
}
long num = bit + (n - 1)/i;
int j = (n - 1) % i + 1;
int result = (int)(num / (Math.pow(10,i - j))) % 10;
return result;
}
}
class Solution {
//时间复杂度O(logn)
//空间复杂度O(1)
public int findNthDigit(int n) {
if(n == 0)return 0;
long bit = 1;
int length = 1;
long size = 9;
while(n > size * length){
bit *= 10;
n -= size * length;
size *= 10;
length ++;
}
long sum = bit - 1 + n / length ;
int s = (int) n % length;
if(s == 0){
return (int) sum % 10;
}else{
long a = (long)Math.pow(10,length - s);
return (int) ((sum + 1) / a) % 10;
}
}
}
class Solution {
public int findNthDigit(int n) {
long sum = 9, i = 1;
int j = 1;
while(n > sum) {
//更新剩余位数的值
n -= sum;
i *= 10;
//j表示几位数
j++;
//更新新的区间的总的位数, 9, 180, 2700, ...
sum = i * j * 9;
}
//得到最后一位所处的那个数字,
String num = "" + (i + (n - 1) / j);
int index = (n - 1) % j;
return num.charAt(index) - '0';
}
}
class Solution {
public int findNthDigit(int n) {
if (n == 0) {
return 0;
}
long bit = 1;
int i = 1;
long count = 9;
while (count < n) {
n -= count;
bit *= 10;
i++;
count = bit * i * 9;
}
long num = bit + (n - 1) / i;
int index = (n - 1) % i + 1;
int res = (int) (num / Math.pow(10, i - index)) % 10;
return res;
}
}
class Solution:
def findNthDigit(self, n: int) -> int:
d, count = 1, 9
while n > d * count:
n -= d * count
d += 1
count *= 10
index = n - 1
start = 10 ** (d - 1)
num = start + index // d
digitIndex = index % d
return num // 10 ** (d - digitIndex - 1) % 10
class Solution {
public int findNthDigit(int n) {
if(n == 0) return 0;
//某个范围的起始值,1,10,100,1000
long bit = 1;
// 位数,个十百千,1就是个,2就是十
int i = 1;
// 二位数count=180(0-99的字符位数),三位数就是2700(0-999的字符位数)
long count = 9;
while(n>count){
// n逐步递减,找到它是属于哪个范围,
n = (int)(n -count);
// 范围起始值逐步*10
bit = bit * 10;
// 位数也递加
i++;
// 计算所有位数,假设3位数, bit = 100, i =3,count=2700,说明0-999有2700个数位
count = bit * i*9;
}
// 找到属于该范围的哪个数,n这个时候已经被减去过了,所以它就是从某个范围的 初始值开始的,比如100或者1000。/i是这个范围的每个数都是i位的
long num = (n - 1) / i + bit;
// 找到数之后计算是这个数的哪一位
int index = (n-1) % i + 1;
// 这里加入i = 8, index = 1,那num除完之后,刚好是一位数。但如果i = 8, index = 7,那Num除完之后,是7位数,对10取余,得到最后一位
int res = (int)(num / Math.pow(10,i-index)) % 10;
return res;
}
}
共以下 21 个回答
定位几位数,定位数字,定位位置
感觉实战的时候应该想不出来
时间:O(logn)
空间:O(1)
“`java
class Solution {
public int findNthDigit(int n) {
//总结规律发现 每个区间段的数字有 9*bit 字符有 9*bit*i i表示这个数字是几位数 bit表示数量级
if(n == 0){
return 0;
}
long bit = 1;
int i = 1;
long count = 9;
}
“`java
class Solution {
public int findNthDigit(int n) {
//总结规律发现 每个区间段的数字有 9*bit 字符有 9*bit*i i表示这个数字是几位数 bit表示数量级
if(n == 0){
return 0;
}
long bit = 1;
int i = 1;
long count = 9;
}
“`java
class Solution {
public int findNthDigit(int n) {
//总结规律发现 每个区间段的数字有 9*bit 字符有 9*bit*i i表示这个数字是几位数 bit表示数量级
if(n == 0){
return 0;
}
long bit = 1;
int i = 1;
long count = 9;
}
“`java
class Solution {
public int findNthDigit(int n) {
//总结规律发现 每个区间段的数字有 9*bit 字符有 9*bit*i i表示这个数字是几位数 bit表示数量级
if(n == 0){
return 0;
}
long bit = 1;
int i = 1;
long count = 9;
}
不会,看的官方题解
时间复杂度为O(lgn)
空间复杂度为O(1)
时间O(logn)
空间O(n)
时间:O(1),空间:O(1)
官方
时间O(logn)
空间O(n)