forked from prateekshyap/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BS_ContguouslyIncreasingNumbers.cpp
69 lines (59 loc) · 1.46 KB
/
BS_ContguouslyIncreasingNumbers.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
65
66
67
68
69
/*
https://binarysearch.com/problems/Contiguously-Increasing-Numbers
Contiguously Increasing Numbers
*/
vector<int> solve_1(int start, int end) {
vector<int> ans;
queue<int> q;
int d, lastDig;
for (int i = 1; i <= 9; i++) q.push(i);
while (!q.empty()) {
d = q.front();
q.pop();
if (d >= start and d <= end) ans.push_back(d);
if (d < end) {
lastDig = d % 10;
if (lastDig != 9) {
q.push(d * 10 + lastDig + 1);
}
}
}
return ans;
}
vector<int> solve_2(int start, int end) {
vector<int> ans;
queue<int> q;
int sz, d;
for (int i = 1; i <= 9; i++) q.push(i);
while (!q.empty()) {
sz = q.size();
while (sz--) {
d = q.front();
q.pop();
if (d >= start and d <= end) ans.push_back(d);
if (d < end) {
if (d % 10 != 9) {
q.push(d * 10 + d % 10 + 1);
}
}
}
}
return ans;
}
vector<int> solve(int start, int end) {
vector<int> ans;
for (int d = 1; d <= 9; d++) {
if (d >= start and d<=end)
ans.push_back(d);
int nd = d+1;
int nnum = d*10 + nd;
while(nd <=9)
{
if (nnum >= start and nnum<=end)
ans.push_back(nnum);
nnum = nnum*10 + ++nd;
}
}
sort(ans.begin(), ans.end());
return ans;
}