Skip to content

Commit fa9fd24

Browse files
committed
feat: add solutions to lc problem: No.0247
No.0247.Strobogrammatic Number II
1 parent 3683e10 commit fa9fd24

File tree

9 files changed

+275
-46
lines changed

9 files changed

+275
-46
lines changed

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

+97-1
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,111 @@
4444
<!-- 这里可写当前语言的特殊实现逻辑 -->
4545

4646
```python
47-
47+
class Solution:
48+
def findStrobogrammatic(self, n: int) -> List[str]:
49+
def dfs(u):
50+
if u == 0:
51+
return ['']
52+
if u == 1:
53+
return ['0', '1', '8']
54+
ans = []
55+
for v in dfs(u - 2):
56+
for l, r in [['1', '1'], ['8', '8'], ['6', '9'], ['9', '6']]:
57+
ans.append(l + v + r)
58+
if u != n:
59+
ans.append('0' + v + '0')
60+
return ans
61+
62+
return dfs(n)
4863
```
4964

5065
### **Java**
5166

5267
<!-- 这里可写当前语言的特殊实现逻辑 -->
5368

5469
```java
70+
class Solution {
71+
private int n;
72+
73+
public List<String> findStrobogrammatic(int n) {
74+
this.n = n;
75+
return dfs(n);
76+
}
77+
78+
private List<String> dfs(int u) {
79+
if (u == 0) {
80+
return Collections.singletonList("");
81+
}
82+
if (u == 1) {
83+
return Arrays.asList("0", "1", "8");
84+
}
85+
List<String> ans = new ArrayList<>();
86+
int[][] pairs = new int[][]{{1, 1}, {8, 8}, {6, 9}, {9, 6}};
87+
for (String v : dfs(u - 2)) {
88+
for (int[] p : pairs) {
89+
ans.add(p[0] + v + p[1]);
90+
}
91+
if (u != n) {
92+
ans.add("0" + v + "0");
93+
}
94+
}
95+
return ans;
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
int n;
106+
vector<string> findStrobogrammatic(int n) {
107+
this->n = n;
108+
return dfs(n);
109+
}
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+
{
118+
for (auto& p : pairs) ans.push_back({p[0] + v + p[1]});
119+
if (u != n) ans.push_back('0' + v + '0');
120+
}
121+
return ans;
122+
}
123+
};
124+
```
55125

126+
### **Go**
127+
128+
```go
129+
func findStrobogrammatic(n int) []string {
130+
var dfs func(int) []string
131+
dfs = func(u int) []string {
132+
if u == 0 {
133+
return []string{""}
134+
}
135+
if u == 1 {
136+
return []string{"0", "1", "8"}
137+
}
138+
var ans []string
139+
pairs := [][]string{{"1", "1"}, {"8", "8"}, {"6", "9"}, {"9", "6"}}
140+
for _, v := range dfs(u - 2) {
141+
for _, p := range pairs {
142+
ans = append(ans, p[0]+v+p[1])
143+
}
144+
if u != n {
145+
ans = append(ans, "0"+v+"0")
146+
}
147+
}
148+
return ans
149+
}
150+
return dfs(n)
151+
}
56152
```
57153

58154
### **...**

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

+97-1
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,109 @@
3030
### **Python3**
3131

3232
```python
33-
33+
class Solution:
34+
def findStrobogrammatic(self, n: int) -> List[str]:
35+
def dfs(u):
36+
if u == 0:
37+
return ['']
38+
if u == 1:
39+
return ['0', '1', '8']
40+
ans = []
41+
for v in dfs(u - 2):
42+
for l, r in [['1', '1'], ['8', '8'], ['6', '9'], ['9', '6']]:
43+
ans.append(l + v + r)
44+
if u != n:
45+
ans.append('0' + v + '0')
46+
return ans
47+
48+
return dfs(n)
3449
```
3550

3651
### **Java**
3752

3853
```java
54+
class Solution {
55+
private int n;
56+
57+
public List<String> findStrobogrammatic(int n) {
58+
this.n = n;
59+
return dfs(n);
60+
}
61+
62+
private List<String> dfs(int u) {
63+
if (u == 0) {
64+
return Collections.singletonList("");
65+
}
66+
if (u == 1) {
67+
return Arrays.asList("0", "1", "8");
68+
}
69+
List<String> ans = new ArrayList<>();
70+
int[][] pairs = new int[][]{{1, 1}, {8, 8}, {6, 9}, {9, 6}};
71+
for (String v : dfs(u - 2)) {
72+
for (int[] p : pairs) {
73+
ans.add(p[0] + v + p[1]);
74+
}
75+
if (u != n) {
76+
ans.add("0" + v + "0");
77+
}
78+
}
79+
return ans;
80+
}
81+
}
82+
```
83+
84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
int n;
90+
vector<string> findStrobogrammatic(int n) {
91+
this->n = n;
92+
return dfs(n);
93+
}
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+
{
102+
for (auto& p : pairs) ans.push_back({p[0] + v + p[1]});
103+
if (u != n) ans.push_back('0' + v + '0');
104+
}
105+
return ans;
106+
}
107+
};
108+
```
39109

110+
### **Go**
111+
112+
```go
113+
func findStrobogrammatic(n int) []string {
114+
var dfs func(int) []string
115+
dfs = func(u int) []string {
116+
if u == 0 {
117+
return []string{""}
118+
}
119+
if u == 1 {
120+
return []string{"0", "1", "8"}
121+
}
122+
var ans []string
123+
pairs := [][]string{{"1", "1"}, {"8", "8"}, {"6", "9"}, {"9", "6"}}
124+
for _, v := range dfs(u - 2) {
125+
for _, p := range pairs {
126+
ans = append(ans, p[0]+v+p[1])
127+
}
128+
if u != n {
129+
ans = append(ans, "0"+v+"0")
130+
}
131+
}
132+
return ans
133+
}
134+
return dfs(n)
135+
}
40136
```
41137

42138
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int n;
4+
vector<string> findStrobogrammatic(int n) {
5+
this->n = n;
6+
return dfs(n);
7+
}
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+
{
16+
for (auto& p : pairs) ans.push_back({p[0] + v + p[1]});
17+
if (u != n) ans.push_back('0' + v + '0');
18+
}
19+
return ans;
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func findStrobogrammatic(n int) []string {
2+
var dfs func(int) []string
3+
dfs = func(u int) []string {
4+
if u == 0 {
5+
return []string{""}
6+
}
7+
if u == 1 {
8+
return []string{"0", "1", "8"}
9+
}
10+
var ans []string
11+
pairs := [][]string{{"1", "1"}, {"8", "8"}, {"6", "9"}, {"9", "6"}}
12+
for _, v := range dfs(u - 2) {
13+
for _, p := range pairs {
14+
ans = append(ans, p[0]+v+p[1])
15+
}
16+
if u != n {
17+
ans = append(ans, "0"+v+"0")
18+
}
19+
}
20+
return ans
21+
}
22+
return dfs(n)
23+
}
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,28 @@
11
class Solution {
2-
Map<Character, Character> map = new HashMap<>();
3-
{
4-
map.put('1', '1');
5-
map.put('0', '0');
6-
map.put('6', '9');
7-
map.put('9', '6');
8-
map.put('8', '8');
9-
}
2+
private int n;
103

114
public List<String> findStrobogrammatic(int n) {
12-
if (n == 1) {
13-
return Arrays.asList("0", "1", "8");
14-
}
15-
List<String> ans = new ArrayList<>();
16-
dfs(n, ans, "");
17-
return ans;
5+
this.n = n;
6+
return dfs(n);
187
}
198

20-
private void dfs(int n, List<String> ans, String tmp) {
21-
if (tmp.length() == (n + 1) / 2) {
22-
fillAns(n, ans, tmp);
23-
return;
9+
private List<String> dfs(int u) {
10+
if (u == 0) {
11+
return Collections.singletonList("");
2412
}
25-
26-
for (char c : map.keySet()) {
27-
int num = c - '0';
28-
// 首位不能是0
29-
if (tmp.length() == 0 && num == 0) {
30-
continue;
13+
if (u == 1) {
14+
return Arrays.asList("0", "1", "8");
15+
}
16+
List<String> ans = new ArrayList<>();
17+
int[][] pairs = new int[][]{{1, 1}, {8, 8}, {6, 9}, {9, 6}};
18+
for (String v : dfs(u - 2)) {
19+
for (int[] p : pairs) {
20+
ans.add(p[0] + v + p[1]);
3121
}
32-
// 奇数的中间位只能是 0 1 8
33-
if (n % 2 != 0 && tmp.length() == n / 2 && !(num == 0 || num == 1 || num == 8)) {
34-
continue;
22+
if (u != n) {
23+
ans.add("0" + v + "0");
3524
}
36-
dfs(n, ans, tmp + num);
3725
}
26+
return ans;
3827
}
39-
40-
private void fillAns(int n, List<String> ans, String tmp) {
41-
char[] a = new char[n];
42-
for (int i = 0; i < tmp.length(); i++) {
43-
a[i] = tmp.charAt(i);
44-
a[n - i - 1] = map.get(tmp.charAt(i));
45-
}
46-
if (n % 2 != 0) {
47-
a[tmp.length() - 1] = tmp.charAt(tmp.length() - 1);
48-
}
49-
ans.add(new String(a));
50-
}
51-
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def findStrobogrammatic(self, n: int) -> List[str]:
3+
def dfs(u):
4+
if u == 0:
5+
return ['']
6+
if u == 1:
7+
return ['0', '1', '8']
8+
ans = []
9+
for v in dfs(u - 2):
10+
for l, r in [['1', '1'], ['8', '8'], ['6', '9'], ['9', '6']]:
11+
ans.append(l + v + r)
12+
if u != n:
13+
ans.append('0' + v + '0')
14+
return ans
15+
16+
return dfs(n)

solution/2100-2199/2151.Maximum Good People Based on Statements/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func maximumGood(statements [][]int) int {
195195
n := len(statements)
196196
check := func(k int) int {
197197
cnt := 0
198-
for i, s := range statements {
198+
for i, s := range statements {
199199
if ((k >> i) & 1) == 1 {
200200
for j := 0; j < n; j++ {
201201
if s[j] < 2 && ((k>>j)&1) != s[j] {

solution/2100-2199/2151.Maximum Good People Based on Statements/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func maximumGood(statements [][]int) int {
182182
n := len(statements)
183183
check := func(k int) int {
184184
cnt := 0
185-
for i, s := range statements {
185+
for i, s := range statements {
186186
if ((k >> i) & 1) == 1 {
187187
for j := 0; j < n; j++ {
188188
if s[j] < 2 && ((k>>j)&1) != s[j] {

solution/2100-2199/2151.Maximum Good People Based on Statements/Solution.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ func maximumGood(statements [][]int) int {
22
n := len(statements)
33
check := func(k int) int {
44
cnt := 0
5-
for i, s := range statements {
5+
for i, s := range statements {
66
if ((k >> i) & 1) == 1 {
77
for j := 0; j < n; j++ {
88
if s[j] < 2 && ((k>>j)&1) != s[j] {

0 commit comments

Comments
 (0)