classSolution { public: vector<string> res; string cur; // numbers would be the same after 180: 0, 1, 8 // numbers would be each other after 180: 6, 9 vector<char> selfReversable = {'0', '1', '8'}; vector<string> findStrobogrammatic(int n){ cur = string(n, '0'); dfs(0, n - 1); return res; }
voiddfs(int start, int end){ if (start > end) { res.push_back(cur); return; } if (start == end) { for (char& c : selfReversable) { cur[start] = c; res.push_back(cur); } return; } for (char& c : selfReversable) { if (start == 0 && c == '0') { continue; } cur[start] = c; cur[end] = c; dfs(start + 1, end - 1); } cur[start] = '6'; cur[end] = '9'; dfs(start + 1, end - 1); cur[start] = '9'; cur[end] = '6'; dfs(start + 1, end - 1); } };