← Home

400. Nth Digit

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
400

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 400. Nth Digit, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

The notes already sitting in the source point us in the right direction:

  • TLE

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are findNthDigit.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//TLE
02class Solution {
03public:
04    int findNthDigit(int n) {
05        string s = "";
06        for(int i = 1; s.size() < n; i++){
07            s += to_string(i);
08        }
09        return s[n-1]-'0';
10    }
11};
12
13//Math
14//https://leetcode.com/problems/nth-digit/discuss/88363/Java-solution
15//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Nth Digit.
16//Memory Usage: 6.1 MB, less than 45.17% of C++ online submissions for Nth Digit.
17class Solution {
18public:
19    int findNthDigit(int n) {
20        int len = 1;
21        long long count = 9; //1 to 9
22        int num = 1;
23        
24        //look through 1-digit numbers, 2-digit numbers, ...
25        while(n > len * count){
26            n -= len*count;
27            ++len;
28            count *= 10;
29            num *= 10;
30        }
31        
32        /*
33        for n = 195,
34        we skip 1*9 + 2*90 = 189 numbers,
35        so now n = 6(index start from 1),
36        what we want is the 2nd 3-digit number,
37        turn it into 0-based index, 
38        it would be 1st(0-based) 3-digit number,
39        we want the mapping:
40        1,2,3 -> 0
41        4,5,6 -> 1
42        and (n-1)/len is just this!
43        */
44        // cout << num << ", " << n << endl;
45        num += (n-1)/len;
46        // cout << num << endl;
47        
48        return to_string(num)[(n-1)%len]-'0';
49    }
50};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.