← Home

1291. Sequential Digits

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

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

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. 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.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are sequentialDigits.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • 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//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Sequential Digits.
02//Memory Usage: 6.6 MB, less than 5.19% of C++ online submissions for Sequential Digits.
03class Solution {
04public:
05    vector<int> sequentialDigits(int low, int high) {
06        int len_low = log10(low)+1;
07        int len_high = log10(high)+1;
08        
09        vector<int> ans;
10        
11        for(int len = len_low; len <= len_high; ++len){
12            int head_start = (len == len_low) ? low/pow(10, len_low-1) : 1;
13            
14            int cur = 0;
15            for(int head = head_start; head + len - 1 <= 9; ++head){
16                cur = 0;
17                for(int i = 0; i < len; ++i){
18                    cur += pow(10, len-1-i) * (head+i);
19                }
20                if(cur >= low && cur <= high) ans.push_back(cur);
21            }
22        }
23        
24        return ans;
25    }
26};

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.