# 247. Strobogrammatic Number II

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.

Example:

``````Input:  n = 2
Output: ["11","69","88","96"]
``````

n = 0:   none

n = 1:   0, 1, 8

n = 2:   11, 69, 88, 96

n = 3:   101, 609, 808, 906, 111, 619, 818, 916, 181, 689, 888, 986

n = 4:   1001, 6009, 8008, 9006, 1111, 6119, 8118, 9116, 1691, 6699, 8698, 9696, 1881, 6889, 8888, 9886, 1961, 6969, 8968, 9966

``````class Solution {
public:
vector<string> findStrobogrammatic(int n) {
return find(n, n);
}
vector<string> find(int m, int n) {
if (m == 0) return {""};
if (m == 1) return {"0", "1", "8"};
vector<string> t = find(m - 2, n), res;
for (auto a : t) {
if (m != n) res.push_back("0" + a + "0");
res.push_back("1" + a + "1");
res.push_back("6" + a + "9");
res.push_back("8" + a + "8");
res.push_back("9" + a + "6");
}
return res;
}
};
``````

``````class Solution {
public:
vector<string> findStrobogrammatic(int n) {
vector<string> one{"0", "1", "8"}, two{""}, res = two;
if (n % 2 == 1) res = one;
for (int i = (n % 2) + 2; i <= n; i += 2) {
vector<string> t;
for (auto a : res) {
if (i != n) t.push_back("0" + a + "0");
t.push_back("1" + a + "1");
t.push_back("6" + a + "9");
t.push_back("8" + a + "8");
t.push_back("9" + a + "6");
}
res = t;
}
return res;
}
};
``````

Github 同步地址：

https://github.com/grandyang/leetcode/issues/247

Strobogrammatic Number

Strobogrammatic Number III

https://leetcode.com/problems/strobogrammatic-number-ii/

https://leetcode.com/problems/strobogrammatic-number-ii/discuss/67280/AC-clean-Java-solution

https://leetcode.com/problems/strobogrammatic-number-ii/discuss/67288/Simple-Java-solution-without-recursion

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation