# [LeetCode] 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.

For example,
Given n = 2, return ["11","69","88","96"].

Hint:

1. Try to use recursion and notice that it should recurse with n - 2 instead of n - 1.

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) {
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;
}
};

|
9月前
|

Leetcode 313. Super Ugly Number

81 1
|
2月前
|

LeetCode 题目 65：有效数字（Valid Number）【python】
LeetCode 题目 65：有效数字（Valid Number）【python】
37 5
|
3月前
|

【LeetCode力扣】单调栈解决Next Greater Number（下一个更大值）问题
【LeetCode力扣】单调栈解决Next Greater Number（下一个更大值）问题
25 0
|
9月前
|

Leetcode Single Number II （面试题推荐）

30 0
|

LeetCode 414. Third Maximum Number

79 0
|

LeetCode 405. Convert a Number to Hexadecimal

74 0
|
API
LeetCode 375. Guess Number Higher or Lower II

93 0
|
API
LeetCode 374. Guess Number Higher or Lower

65 0
|

LeetCode 321. Create Maximum Number

57 0
|

LeetCode 313. Super Ugly Number

83 0