Skip to content

Commit ceafac4

Browse files
committed
feat: add solutions to lc problem: No.0247
No.0247.Strobogrammatic Number II
1 parent e127845 commit ceafac4

File tree

5 files changed

+64
-54
lines changed

5 files changed

+64
-54
lines changed

solution/0200-0299/0247.Strobogrammatic Number II/README.md

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@
3737

3838
<!-- 这里可写通用的实现逻辑 -->
3939

40+
**方法一:递归**
41+
42+
若长度为 $1$,则中心对称数只有 $0, 1, 8$;若长度为 $2$,则中心对称数只有 $11, 69, 88, 96$。
43+
44+
我们设计递归函数 $dfs(u)$,其返回长度为 $u$ 的中心对称数。答案为 $dfs(n)$。
45+
46+
若 $u$ 为 $0$,返回包含一个空串的列表,即 `[""]`;若 $u$ 为 $1$,返回列表 `["0", "1", "8"]`
47+
48+
若 $u$ 大于 $1$,我们对长度为 $u - 2$ 的所有中心对称数进行遍历,对于每个中心对称数 $v$,在其左右两侧分别添加 $1, 8, 6, 9$,即可得到长度为 `u` 的中心对称数。
49+
50+
注意,如果 $u\neq n$,我们还可以在中心对称数的左右两侧分别添加 $0$。
51+
52+
最终,我们将所有长度为 $n$ 的中心对称数返回即可。
53+
54+
时间复杂度为 $O(4\times 2^ n)$。
55+
4056
<!-- tabs:start -->
4157

4258
### **Python3**
@@ -53,7 +69,7 @@ class Solution:
5369
return ['0', '1', '8']
5470
ans = []
5571
for v in dfs(u - 2):
56-
for l, r in [['1', '1'], ['8', '8'], ['6', '9'], ['9', '6']]:
72+
for l, r in ('11', '88', '69', '96'):
5773
ans.append(l + v + r)
5874
if u != n:
5975
ans.append('0' + v + '0')
@@ -68,6 +84,7 @@ class Solution:
6884

6985
```java
7086
class Solution {
87+
private static final int[][] PAIRS = {{1, 1}, {8, 8}, {6, 9}, {9, 6}};
7188
private int n;
7289

7390
public List<String> findStrobogrammatic(int n) {
@@ -83,13 +100,12 @@ class Solution {
83100
return Arrays.asList("0", "1", "8");
84101
}
85102
List<String> ans = new ArrayList<>();
86-
int[][] pairs = new int[][] {{1, 1}, {8, 8}, {6, 9}, {9, 6}};
87103
for (String v : dfs(u - 2)) {
88-
for (int[] p : pairs) {
104+
for (var p : PAIRS) {
89105
ans.add(p[0] + v + p[1]);
90106
}
91107
if (u != n) {
92-
ans.add("0" + v + "0");
108+
ans.add(0 + v + 0);
93109
}
94110
}
95111
return ans;
@@ -102,23 +118,21 @@ class Solution {
102118
```cpp
103119
class Solution {
104120
public:
105-
int n;
121+
const vector<pair<char, char>> pairs = {{'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'}};
122+
106123
vector<string> findStrobogrammatic(int n) {
107-
this->n = n;
124+
function<vector<string>(int)> dfs = [&](int u) {
125+
if (u == 0) return vector<string>{""};
126+
if (u == 1) return vector<string>{"0", "1", "8"};
127+
vector<string> ans;
128+
for (auto& v : dfs(u - 2)) {
129+
for (auto& [l, r] : pairs) ans.push_back(l + v + r);
130+
if (u != n) ans.push_back('0' + v + '0');
131+
}
132+
return ans;
133+
};
108134
return dfs(n);
109135
}
110-
111-
vector<string> dfs(int u) {
112-
if (u == 0) return {""};
113-
if (u == 1) return {"0", "1", "8"};
114-
vector<string> ans;
115-
vector<vector<char>> pairs = {{'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'}};
116-
for (string v : dfs(u - 2)) {
117-
for (auto& p : pairs) ans.push_back({p[0] + v + p[1]});
118-
if (u != n) ans.push_back('0' + v + '0');
119-
}
120-
return ans;
121-
}
122136
};
123137
```
124138

solution/0200-0299/0247.Strobogrammatic Number II/README_EN.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Solution:
3939
return ['0', '1', '8']
4040
ans = []
4141
for v in dfs(u - 2):
42-
for l, r in [['1', '1'], ['8', '8'], ['6', '9'], ['9', '6']]:
42+
for l, r in ('11', '88', '69', '96'):
4343
ans.append(l + v + r)
4444
if u != n:
4545
ans.append('0' + v + '0')
@@ -52,6 +52,7 @@ class Solution:
5252

5353
```java
5454
class Solution {
55+
private static final int[][] PAIRS = {{1, 1}, {8, 8}, {6, 9}, {9, 6}};
5556
private int n;
5657

5758
public List<String> findStrobogrammatic(int n) {
@@ -67,13 +68,12 @@ class Solution {
6768
return Arrays.asList("0", "1", "8");
6869
}
6970
List<String> ans = new ArrayList<>();
70-
int[][] pairs = new int[][] {{1, 1}, {8, 8}, {6, 9}, {9, 6}};
7171
for (String v : dfs(u - 2)) {
72-
for (int[] p : pairs) {
72+
for (var p : PAIRS) {
7373
ans.add(p[0] + v + p[1]);
7474
}
7575
if (u != n) {
76-
ans.add("0" + v + "0");
76+
ans.add(0 + v + 0);
7777
}
7878
}
7979
return ans;
@@ -86,23 +86,21 @@ class Solution {
8686
```cpp
8787
class Solution {
8888
public:
89-
int n;
89+
const vector<pair<char, char>> pairs = {{'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'}};
90+
9091
vector<string> findStrobogrammatic(int n) {
91-
this->n = n;
92+
function<vector<string>(int)> dfs = [&](int u) {
93+
if (u == 0) return vector<string>{""};
94+
if (u == 1) return vector<string>{"0", "1", "8"};
95+
vector<string> ans;
96+
for (auto& v : dfs(u - 2)) {
97+
for (auto& [l, r] : pairs) ans.push_back(l + v + r);
98+
if (u != n) ans.push_back('0' + v + '0');
99+
}
100+
return ans;
101+
};
92102
return dfs(n);
93103
}
94-
95-
vector<string> dfs(int u) {
96-
if (u == 0) return {""};
97-
if (u == 1) return {"0", "1", "8"};
98-
vector<string> ans;
99-
vector<vector<char>> pairs = {{'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'}};
100-
for (string v : dfs(u - 2)) {
101-
for (auto& p : pairs) ans.push_back({p[0] + v + p[1]});
102-
if (u != n) ans.push_back('0' + v + '0');
103-
}
104-
return ans;
105-
}
106104
};
107105
```
108106

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
class Solution {
22
public:
3-
int n;
3+
const vector<pair<char, char>> pairs = {{'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'}};
4+
45
vector<string> findStrobogrammatic(int n) {
5-
this->n = n;
6+
function<vector<string>(int)> dfs = [&](int u) {
7+
if (u == 0) return vector<string>{""};
8+
if (u == 1) return vector<string>{"0", "1", "8"};
9+
vector<string> ans;
10+
for (auto& v : dfs(u - 2)) {
11+
for (auto& [l, r] : pairs) ans.push_back(l + v + r);
12+
if (u != n) ans.push_back('0' + v + '0');
13+
}
14+
return ans;
15+
};
616
return dfs(n);
717
}
8-
9-
vector<string> dfs(int u) {
10-
if (u == 0) return {""};
11-
if (u == 1) return {"0", "1", "8"};
12-
vector<string> ans;
13-
vector<vector<char>> pairs = {{'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'}};
14-
for (string v : dfs(u - 2)) {
15-
for (auto& p : pairs) ans.push_back({p[0] + v + p[1]});
16-
if (u != n) ans.push_back('0' + v + '0');
17-
}
18-
return ans;
19-
}
2018
};

solution/0200-0299/0247.Strobogrammatic Number II/Solution.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class Solution {
2+
private static final int[][] PAIRS = {{1, 1}, {8, 8}, {6, 9}, {9, 6}};
23
private int n;
34

45
public List<String> findStrobogrammatic(int n) {
@@ -14,13 +15,12 @@ private List<String> dfs(int u) {
1415
return Arrays.asList("0", "1", "8");
1516
}
1617
List<String> ans = new ArrayList<>();
17-
int[][] pairs = new int[][] {{1, 1}, {8, 8}, {6, 9}, {9, 6}};
1818
for (String v : dfs(u - 2)) {
19-
for (int[] p : pairs) {
19+
for (var p : PAIRS) {
2020
ans.add(p[0] + v + p[1]);
2121
}
2222
if (u != n) {
23-
ans.add("0" + v + "0");
23+
ans.add(0 + v + 0);
2424
}
2525
}
2626
return ans;

solution/0200-0299/0247.Strobogrammatic Number II/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def dfs(u):
77
return ['0', '1', '8']
88
ans = []
99
for v in dfs(u - 2):
10-
for l, r in [['1', '1'], ['8', '8'], ['6', '9'], ['9', '6']]:
10+
for l, r in ('11', '88', '69', '96'):
1111
ans.append(l + v + r)
1212
if u != n:
1313
ans.append('0' + v + '0')

0 commit comments

Comments
 (0)