-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathsolution.cpp
More file actions
35 lines (28 loc) · 931 Bytes
/
Copy pathsolution.cpp
File metadata and controls
35 lines (28 loc) · 931 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
class Solution
{
public:
vector<int> sequentialDigits(int low, int high)
{
// String containing all consecutive digits
string digits = "123456789";
// Result array
vector<int> ans;
// Number of digits in low and high
int minLen = to_string(low).size();
int maxLen = to_string(high).size();
// Try every possible length
for (int len = minLen; len <= maxLen; len++)
{
// Generate every substring of current length
for (int start = 0; start + len <= 9; start++)
{
// Convert substring into an integer
int num = stoi(digits.substr(start, len));
// Keep only numbers inside the range
if (num >= low && num <= high)
ans.push_back(num);
}
}
return ans;
}
};