A stepping number is a fascinating mathematical concept where each digit in the number has a very special relationship with its neighbors. Specifically, every pair of adjacent digits must have an absolute difference of exactly 1.
Think of it like climbing stairs - you can only go up or down by exactly one step at a time! For example:
321is a stepping number: |3-2| = 1, |2-1| = 1 ✓4321is a stepping number: |4-3| = 1, |3-2| = 1, |2-1| = 1 ✓421is NOT a stepping number: |4-2| = 2 ✗
Your task is to find all stepping numbers within a given range [low, high] (inclusive) and return them in sorted order.
Goal: Generate all valid stepping numbers in the range efficiently.
Input: Two integers low and high representing the inclusive range.
Output: A sorted list of all stepping numbers in the range.
Input & Output
Visualization
Time & Space Complexity
Where k is the number of stepping numbers ≤ high. We generate each valid number once.
Space for the queue and result array containing k stepping numbers
Constraints
- 0 ≤ low ≤ high ≤ 2 × 109
- The answer is guaranteed to fit within a 32-bit integer
- Single digits (0-9) are considered stepping numbers