题目描述:
中心对称数是指一个数字在旋转了 180 度之后看起来依旧相同的数字(或者上下颠倒地看)。
找到所有长度为 n 的中心对称数。
示例 :
输入: n = 2
输出: [“11”,“69”,“88”,“96”]
方法1:
主要思路:
(1)先找出前半段的,再根据 n 的奇偶,来处理后半段内容;
class Solution {
public:
//根据奇偶,找出前半段的内容
void helper(vector<string>& res,string& path, int n,const vector<char>&lables){
if(n<path.size()){
return;
}
if(n==path.size()){
res.push_back(path);
return;
}
for(int i=0;i<lables.size();++i){
path.push_back(lables[i]);
helper(res,path,n,lables);
path.pop_back();
}
}
vector<string> findStrobogrammatic(int n) {
if(n==1){//处理特殊的情形
return {"0","1","8"};
}
//对应的映射
unordered_map<char,char> mp;
mp['0']='0';
mp['1']='1';
mp['8']='8';
mp['6']='9';
mp['9']='6';
//需要用的字符
vector<char> lables={'0','1','6','8','9'};
vector<string> res;
for(int i=1;i<lables.size();++i){//从1开始,跳过0
string path;
path+=lables[i];
helper(res,path,n/2,lables);
}
int len=res.size();//大小
for(int i=0;i<len;++i){
if(n%2==1){//长度是奇数的情形
string str;
//找出后半段
for(int pos=res[i].size()-1;pos>=0;--pos){
str+=mp[res[i][pos]];
}
res.push_back(res[i]+"1"+str);
res.push_back(res[i]+"8"+str);
res[i]+="0"+str;
}
else{//长度是奇数的情形
for(int pos=res[i].size()-1;pos>=0;--pos){
res[i]+=mp[res[i][pos]];
}
}
}
return res;
}
};