-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathStrobogrammatic_Number_II.cpp
30 lines (30 loc) · 1.07 KB
/
Strobogrammatic_Number_II.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
class Solution {
public:
void findStrobogrammaticRecur(int left, int right, string& str, vector<string>& result) {
if(left > right) {
result.push_back(str);
return;
}
if((left == 0 and left == right) or left > 0) {
str[left] = str[right] = '0';
findStrobogrammaticRecur(left + 1, right - 1, str, result);
}
str[left] = str[right] = '1';
findStrobogrammaticRecur(left + 1, right - 1, str, result);
str[left] = str[right] = '8';
findStrobogrammaticRecur(left + 1, right - 1, str, result);
if(left < right) {
str[left] = '6'; str[right] = '9';
findStrobogrammaticRecur(left + 1, right - 1, str, result);
swap(str[left], str[right]);
findStrobogrammaticRecur(left + 1, right - 1, str, result);
}
}
vector<string> findStrobogrammatic(int n) {
vector<string> result;
string str;
str.resize(n);
findStrobogrammaticRecur(0, n - 1, str, result);
return result;
}
};