[LeetCode] Sequential Digits

1291. Sequential Digits

An integer has sequential digits if and only if each digit in the number is one more than the previous digit.

Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public:
vector<int> sequentialDigits(int low, int high) {
vector<int> res;
queue<long long> q;
q.push(12);
q.push(23);
q.push(34);
q.push(45);
q.push(56);
q.push(67);
q.push(78);
q.push(89);
while(!q.empty()) {
auto n = q.front(); q.pop();
if(low <= n and n <= high) {
res.push_back(n);
}
if(n <= high) {
auto last = n % 10 + 1;
if(last < 10) q.push(n * 10 + last);
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/07/PS/LeetCode/sequential-digits/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.