01 · Question
A stepping number is an integer such that all of its adjacent digits have an absolute difference of exactly 1.
- For example,
321is a stepping number while421is not.
Given two integers low and high, return a sorted list of all the stepping numbers in the inclusive range [low, high].
02 · Analysis
Start from nonzero leading digits and extend x only with last_digit-1 or last_digit+1 when in 0..9. Each constructed number has one unique parent obtained by removing its last digit, so no duplicates arise. Stop beyond high and include 0 separately if the interval contains it. For M generated numbers and R outputs, time is O(M + R log(R+1)) including output sorting; recursion depth is the number of digits, and output uses O(R) space.
03 · Solution