400. Nth Digit

LeetCode medium original: C# #csharp #leetcode #medium #search #string
Task text is translated from Russian for the selected interface language. Code is left unchanged.

given integer n, вернуть n-ю цифру бесконечной последовательности чисел [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...].

Example:

Input: n = 3

Output: 3

C# solution

matched/original
public class Solution {
    public int FindNthDigit(int n) {
        long length = 1, count = 9, start = 1;
        
        while (n > length * count) {
            n -= (int)(length * count);
            length++;
            count *= 10;
            start *= 10;
        }
        
        start += (n - 1) / length;
        string s = start.ToString();
        return s[(n - 1) % (int)length] - '0';
    }
}

C++ solution

auto-draft, review before submit
#include <bits/stdc++.h>
using namespace std;

// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
    public int FindNthDigit(int n) {
        long length = 1, count = 9, start = 1;
        
        while (n > length * count) {
            n -= (int)(length * count);
            length++;
            count *= 10;
            start *= 10;
        }
        
        start += (n - 1) / length;
        string s = start.ToString();
        return s[(n - 1) % (int)length] - '0';
    }
}

Java solution

matched/original
class Solution {
    public int findNthDigit(int n) {
        int length = 1;
        long count = 9;
        int start = 1;
        
        while (n > length * count) {
            n -= length * count;
            length++;
            count *= 10;
            start *= 10;
        }
        
        start += (n - 1) / length;
        String s = Integer.toString(start);
        return s.charAt((n - 1) % length) - '0';
    }
}

JavaScript solution

matched/original
class Solution {
    findNthDigit(n) {
        let length = 1, count = 9, start = 1;
        
        while (n > length * count) {
            n -= length * count;
            length++;
            count *= 10;
            start *= 10;
        }
        
        start += Math.floor((n - 1) / length);
        const s = start.toString();
        return parseInt(s[(n - 1) % length]);
    }
}

Python solution

matched/original
class Solution:
    def findNthDigit(self, n: int) -> int:
        length = 1
        count = 9
        start = 1
        
        while n > length * count:
            n -= length * count
            length += 1
            count *= 10
            start *= 10
        
        start += (n - 1) // length
        s = str(start)
        return int(s[(n - 1) % length])

Go solution

matched/original
func findNthDigit(n int) int {
    length, count, start := 1, 9, 1
    
    for n > length * count {
        n -= length * count
        length++
        count *= 10
        start *= 10
    }
    
    start += (n - 1) / length
    s := strconv.Itoa(start)
    return int(s[(n - 1) % length] - '0')
}

Algorithm

Definition диапазона:

Начните с определения количества цифр в числах текущего диапазона (1-9, 10-99, 100-999 и т.д.).

Уменьшайте значение n, вычитая количество цифр в текущем диапазоне, пока не найдете диапазон, в который попадает n-я цифра.

Нахождение конкретного числа:

Когда определите диапазон, find точное number, содержащее n-ю цифру.

Определите индекс цифры в этом числе.

Возвращение n-й цифры:

Извлеките и return n-ю цифру из найденного числа.

😎

Vacancies for this task

Active vacancies with overlapping task tags are shown.

All vacancies
There are no active vacancies yet.