-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathSequential Digits.cpp
63 lines (48 loc) · 1.51 KB
/
Sequential Digits.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Solution by Rahul Surana
***********************************************************
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.
***********************************************************
*/
/*
Easy Solution
class Solution {
public:
vector<int> sequentialDigits(int low, int high) {
vector<int> ans;
string s = "123456789";
int st = log10(low), en = log10(high)+1;
for(int i = st; i < en; i++){
for(int j = 0; j < 9-i; j++){
int z = stoi(s.substr(j,i+1));
if(z >= low && z <= high) ans.push_back(z);
}
}
return ans;
}
};
*/
#include <bits/stdc++.h>
class Solution {
public:
vector<int> sequentialDigits(int low, int high) {
vector<int> ans;
int l = to_string((low)).length(),h = to_string((high)).length();
for(int i = l;i <= h; i++){
int x = 1;
while(true){
long z = x;
int j = 1;
while(j < i) { z*=10; z+= x+j; j++; }
if(z >= low && z <= high){
ans.push_back(z);
}
else if(z > high) break;
x++;
if(x>9-i+1) break;
}
}
return ans;
}
};