-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1291SequentialDigits.cpp
64 lines (57 loc) · 1.4 KB
/
1291SequentialDigits.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
64
class Solution {
public:
int number(vector<int> &temp){
int ans=0;
for(int i=0;i<temp.size();++i){
ans=ans*10+temp[i];
}
// cout<<ans<<endl;
return ans;
}
vector<int> sequentialDigits(int low, int high) {
vector<int> ans;
vector<int> temp;
int count=0,lowTemp=low;
while(lowTemp>0){
count++;
lowTemp/=10;
}
for(int i=1;i<=count;i++){
temp.push_back(i);
}
while(1){
int check=number(temp);
if(check>=low) break;
if(temp[temp.size()-1]%10==9){
for(int j=1;j<=temp.size();j++){
temp[j-1]=j;
}
temp.push_back(temp.size()+1);
}
else{
for(int j=0;j<temp.size();j++){
++temp[j];
}
}
}
while(1){
int check=number(temp);
if(check<=high){
ans.push_back(check);
}
else break;
if(ans[ans.size()-1]%10==9){
for(int j=1;j<=temp.size();j++){
temp[j-1]=j;
}
temp.push_back(temp.size()+1);
}
else{
for(int j=0;j<temp.size();j++){
++temp[j];
}
}
}
return ans;
}
};