forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrobogrammatic-number-ii.cpp
More file actions
32 lines (28 loc) · 917 Bytes
/
strobogrammatic-number-ii.cpp
File metadata and controls
32 lines (28 loc) · 917 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
// Time: O(n^2 * 5^(n/2))
// Space: O(n)
class Solution {
public:
vector<string> findStrobogrammatic(int n) {
return findStrobogrammaticRecu(n, n);
}
vector<string> findStrobogrammaticRecu(const int n, int k) {
if (k == 0) {
return {""};
} else if (k == 1) {
return {"0", "1", "8"};
}
vector<string> result;
for (const auto& num : findStrobogrammaticRecu(n, k - 2)) {
for (const auto& kvp : lookup) {
if (n != k || kvp.first != "0") {
result.emplace_back(kvp.first + num + kvp.second);
}
}
}
return result;
}
private:
const unordered_map<string, string> lookup{{"0", "0"}, {"1", "1"},
{"6", "9"}, {"8", "8"},
{"9", "6"}};
};