Skip to content

Commit 71adeab

Browse files
authored
Create sequential-digits.cpp
1 parent 3bb96c2 commit 71adeab

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

C++/sequential-digits.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time: O((8 + 1) * 8 / 2) = O(1)
2+
// Space: O(8) = O(1)
3+
4+
class Solution {
5+
public:
6+
vector<int> sequentialDigits(int low, int high) {
7+
vector<int> result;
8+
queue<int> q{{1, 2, 3, 4, 5, 6, 7, 8}};
9+
while (!q.empty()) {
10+
const auto num = q.front(); q.pop();
11+
if (num > high) {
12+
continue;
13+
}
14+
if (low <= num) {
15+
result.emplace_back(num);
16+
}
17+
if (num % 10 + 1 < 10) {
18+
q.emplace(num * 10 + num % 10 + 1);
19+
}
20+
}
21+
return result;
22+
}
23+
};

0 commit comments

Comments
 (0)