[InterviewBit] Stepping Numbers

Stepping Numbers

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void helper(int l, int r, int now, vector<int>& res) {
if(l <= now and now <= r) res.push_back(now);
if(r < now) return;
int b = now % 10;
if(0 <= b + 1 and b + 1 <= 9) helper(l,r,now * 10 + b + 1, res);
if(0 <= b - 1 and b - 1 <= 9) helper(l,r,now * 10 + b - 1, res);
}
vector<int> Solution::stepnum(int A, int B) {
vector<int> res;
for(int i = 1; i <= 9; i++) helper(A,B,i,res);
if(A == 0) res.push_back(0);
sort(begin(res),end(res));
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/08/PS/interviewbit/stepping-numbers/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.