1215. Stepping Numbers

A Stepping Number is an integer such that all of its adjacent digits have an absolute difference of exactly `1`. For example, `321` is a Stepping Number while `421` is not.

Given two integers `low` and `high`, find and return a sorted list of all the Stepping Numbers in the range `[low, high]` inclusive.

Example 1:

```Input: low = 0, high = 21
Output: [0,1,2,3,4,5,6,7,8,9,10,12,21]
```

Constraints:

• `0 <= low <= high <= 2 * 10^9`

1215. Stepping Numbers
``````struct Solution;

impl Solution {
fn count_stepping_numbers(low: i32, high: i32) -> Vec<i32> {
let mut res = vec![];
for i in 0..10 {
Self::dfs(i, i, &mut res, low as i64, high as i64);
}
res.sort_unstable();
res
}

fn dfs(last_digit: i64, cur: i64, all: &mut Vec<i32>, low: i64, high: i64) {
if cur >= low && cur <= high {
all.push(cur as i32);
}
if cur == 0 || cur > high {
return;
}
if last_digit > 0 {
Self::dfs(last_digit - 1, cur * 10 + last_digit - 1, all, low, high);
}
if last_digit < 9 {
Self::dfs(last_digit + 1, cur * 10 + last_digit + 1, all, low, high);
}
}
}

#[test]
fn test() {
let low = 0;
let high = 21;
let res = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 21];
assert_eq!(Solution::count_stepping_numbers(low, high), res);
}
``````