题目:
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"]
.
Hint:
- Try to use recursion and notice that it should recurse with n - 2 instead of n - 1.
C++版和Java版是递归,Python版是循环。
C++版:
class Solution {
public:
vector<string> findStrobogrammatic(int n) {
vector<string> results;
if(n == 0)
return results;
unordered_map<char, string> d;
d.insert(pair<char, string>('0', "0"));
d.insert(pair<char, string>('1', "1"));
d.insert(pair<char, string>('6', "9"));
d.insert(pair<char, string>('8', "8"));
d.insert(pair<char, string>('9', "6"));
char s[] = {'0', '1', '8', '6', '9'};
int mid = n / 2;
string result = "";
bool oneMore = (bool)(n % 2);
recursion(d, results, result, mid, 0, s, oneMore);
return results;
}
void recursion(unordered_map<char, string> &d, vector<string> &results, string result, int recur, int iter, char* s, bool &oneMore) {
if(iter > recur) {
for(int i = 0; i < 3; i++) {
results.push_back(result.substr(0, recur) + s[i] + result.substr(recur));
}
return;
}
if(iter == recur) {
if(oneMore) {
recursion(d, results, result, recur, iter + 1, s, oneMore);
} else {
results.push_back(result);
}
return;
}
if(iter == 0) {
for(int i = 1; i < 5; i++) {
recursion(d, results, s[i] + d[s[i]], recur, iter + 1, s, oneMore);
}
} else {
for(int i = 0; i < 5; i++) {
recursion(d, results, result.substr(0, iter) + s[i] + d[s[i]] + result.substr(iter), recur, iter + 1, s, oneMore);
}
}
}
};
Java版:
public class Solution {
public List<String> findStrobogrammatic(int n) {
List<String> results = new ArrayList<>();
if(n == 0)
return results;
String result = "";
HashMap<Character, String> d = new HashMap<>();
d.put('0', "0");
d.put('1', "1");
d.put('6', "9");
d.put('8', "8");
d.put('9', "6");
String s = "01869";
int mid = n / 2;
int oneMore = n % 2;
recursion(d, results, result, s, mid, 0, oneMore);
return results;
}
public void recursion(HashMap<Character, String> d, List<String> results, String result, String s, int recur, int iter, int oneMore) {
if(iter > recur) {
for(int i = 0; i < 3; i++) {
results.add(result.substring(0, recur) + s.charAt(i) + result.substring(recur));
}
return;
}
if(iter == recur) {
if(oneMore == 1) {
recursion(d, results, result, s, recur, iter + 1, oneMore);
} else {
String local = result;
results.add(local);
}
return;
}
if(iter == 0) {
for(int i = 1; i < 5; i++) {
recursion(d, results, s.charAt(i) + d.get(s.charAt(i)), s, recur, iter + 1, oneMore);
}
} else {
for(int i = 0; i < 5; i++) {
recursion(d, results, result.substring(0, iter) + s.charAt(i) + d.get(s.charAt(i)) + result.substring(iter), s, recur, iter + 1, oneMore);
}
}
}
}
Python版:
from copy import copy
class Solution(object):
def findStrobogrammatic(self, n):
"""
:type n: int
:rtype: List[str]
"""
s = ['0', '1', '8', '6', '9']
d = {'0': '0', '1': '1', '6': '9', '8': '8', '9': '6'}
ret1, ret2 = [""], []
if n == 0:
return ret1
mid = n / 2
i = 0
while i < mid:
if i == 0:
for j in range(1, len(s)):
ret2.append(s[j] + d[s[j]])
else:
for j in ret1:
for k in s:
ret2.append(j[:i] + k + d[k] + j[i:])
ret1 = copy(ret2)
ret2 = []
i += 1
if n % 2 != 0:
for j in ret1:
for k in range(3):
ret2.append(j[:i] + s[k] + j[i:])
ret1 = copy(ret2)
return ret1