-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0033_P1291_Sequential_Digits.cpp
More file actions
37 lines (34 loc) · 984 Bytes
/
0033_P1291_Sequential_Digits.cpp
File metadata and controls
37 lines (34 loc) · 984 Bytes
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
27
28
29
30
31
32
33
34
35
36
37
/*
Day: 33
Problem Number: 1291 (https://leetcode.com/problems/sequential-digits)
Date: 02-02-2024
Description:
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.
Example 1:
Input: low = 100, high = 300
Output: [123, 234]
Example 2:
Input: low = 1000, high = 13000
Output: [1234, 2345, 3456, 4567, 5678, 6789, 12345]
Constraints:
* 10 <= low <= high <= 10^9
Code: */
class Solution {
public:
vector<int> sequentialDigits(int low, int high) {
vector<int> ans;
for (int i = 1; i < 9; ++i) {
int x = i;
for (int j = i + 1; j < 10; ++j) {
x = x * 10 + j;
if (x >= low && x <= high) {
ans.push_back(x);
}
}
}
sort(ans.begin(), ans.end());
return ans;
}
};