A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
Find all strobogrammatic numbers that are of length = n.
For example,
Given n = 2, return ["11","69","88","96"]
.
1 class Solution {
2 public:
3 char num[5] = {'0', '1', '6', '8', '9'};
4 char rotate(char num) {
5 if (num == '0' || num == '1' || num == '8')
6 return num;
7 else if (num == '6') return '9';
8 else return '6';
9 }
10 void help(vector<string>& res, string& cand, int curInd) {
11 if (cand.size() & 1 && curInd == (cand.size() - 1) / 2) {
12 cand[curInd] = '0'; res.push_back(cand);
13 cand[curInd] = '1'; res.push_back(cand);
14 cand[curInd] = '8'; res.push_back(cand);
15 } else {
16 int st = curInd ? 0 : 1;
17 for (; st < 5; st++) {
18 cand[curInd] = num[st];
19 cand[cand.size() - curInd - 1] = rotate(num[st]);
20 if (curInd == (cand.size() - 1) / 2) res.push_back(cand);
21 else help(res, cand, curInd + 1);
22 }
23 }
24 }
25 vector<string> findStrobogrammatic(int n) {
26 vector<string> res;
27 if (n < 1) return res;
28 string cand(n, '0');
29 help(res, cand, 0);
30 return res;
31 }
32 };