-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path248.cpp
57 lines (57 loc) · 2.18 KB
/
248.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
__________________________________________________________________________________________________
class Solution {
public:
int strobogrammaticInRange(string low, string high) {
int res = 0;
for (int i = low.size(); i <= high.size(); ++i) {
find(low, high, "", i, res);
find(low, high, "0", i, res);
find(low, high, "1", i, res);
find(low, high, "8", i, res);
}
return res;
}
void find(string low, string high, string path, int len, int &res) {
if (path.size() >= len) {
if (path.size() != len || (len != 1 && path[0] == '0')) return;
if ((len == low.size() && path.compare(low) < 0) || (len == high.size() && path.compare(high) > 0)) {
return;
}
++res;
}
find(low, high, "0" + path + "0", len, res);
find(low, high, "1" + path + "1", len, res);
find(low, high, "6" + path + "9", len, res);
find(low, high, "8" + path + "8", len, res);
find(low, high, "9" + path + "6", len, res);
}
};
__________________________________________________________________________________________________
class Solution {
public:
int strobogrammaticInRange(string low, string high) {
int res = 0;
find(low, high, "", res);
find(low, high, "0", res);
find(low, high, "1", res);
find(low, high, "8", res);
return res;
}
void find(string low, string high, string w, int &res) {
if (w.size() >= low.size() && w.size() <= high.size()) {
if (w.size() == high.size() && w.compare(high) > 0) {
return;
}
if (!(w.size() > 1 && w[0] == '0') && !(w.size() == low.size() && w.compare(low) < 0)) {
++res;
}
}
if (w.size() + 2 > high.size()) return;
find(low, high, "0" + w + "0", res);
find(low, high, "1" + w + "1", res);
find(low, high, "6" + w + "9", res);
find(low, high, "8" + w + "8", res);
find(low, high, "9" + w + "6", res);
}
};
__________________________________________________________________________________________________