Skip to content

Commit 2972e92

Browse files
committed
feat: add solutions to lc problem: No.0248
No.0248.Strobogrammatic Number III
1 parent ceafac4 commit 2972e92

File tree

7 files changed

+431
-3
lines changed

7 files changed

+431
-3
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@
5151

5252
最终,我们将所有长度为 $n$ 的中心对称数返回即可。
5353

54-
时间复杂度为 $O(4\times 2^ n)$。
54+
时间复杂度为 $O(2^{n+2})$。
55+
56+
相似题目:[248. 中心对称数 III](/solution/0200-0299/0248.Strobogrammatic%20Number%20III/README.md)
5557

5658
<!-- tabs:start -->
5759

solution/0200-0299/0248.Strobogrammatic Number III/README.md

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,180 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
**方法一:递归**
47+
48+
若长度为 $1$,则中心对称数只有 $0, 1, 8$;若长度为 $2$,则中心对称数只有 $11, 69, 88, 96$。
49+
50+
我们设计递归函数 $dfs(u)$,其返回长度为 $u$ 的中心对称数。
51+
52+
若 $u$ 为 $0$,返回包含一个空串的列表,即 `[""]`;若 $u$ 为 $1$,返回列表 `["0", "1", "8"]`
53+
54+
若 $u$ 大于 $1$,我们对长度为 $u - 2$ 的所有中心对称数进行遍历,对于每个中心对称数 $v$,在其左右两侧分别添加 $1, 8, 6, 9$,即可得到长度为 $u$ 的中心对称数。
55+
56+
注意,如果 $u\neq n$,我们还可以在中心对称数的左右两侧分别添加 $0$。
57+
58+
设 $low$ 和 $high$ 的长度分别为 $a$ 和 $b$。
59+
60+
接下来,我们在 $[a,..b]$ 范围内遍历所有长度,对于每个长度 $n$,我们获取所有中心对称数 $dfs(n)$,然后判断是否在 $[low, high]$ 范围内,若在,答案加一。
61+
62+
时间复杂度为 $O(2^{n+2}\times \log n)$。
63+
64+
相似题目:[247. 中心对称数 II](/solution/0200-0299/0247.Strobogrammatic%20Number%20II/README.md)
65+
4666
<!-- tabs:start -->
4767

4868
### **Python3**
4969

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

5272
```python
53-
73+
class Solution:
74+
def strobogrammaticInRange(self, low: str, high: str) -> int:
75+
def dfs(u):
76+
if u == 0:
77+
return ['']
78+
if u == 1:
79+
return ['0', '1', '8']
80+
ans = []
81+
for v in dfs(u - 2):
82+
for l, r in ('11', '88', '69', '96'):
83+
ans.append(l + v + r)
84+
if u != n:
85+
ans.append('0' + v + '0')
86+
return ans
87+
88+
a, b = len(low), len(high)
89+
low, high = int(low), int(high)
90+
ans = 0
91+
for n in range(a, b + 1):
92+
for s in dfs(n):
93+
if low <= int(s) <= high:
94+
ans += 1
95+
return ans
5496
```
5597

5698
### **Java**
5799

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

60102
```java
103+
class Solution {
104+
private static final int[][] PAIRS = {{1, 1}, {8, 8}, {6, 9}, {9, 6}};
105+
private int n;
106+
107+
public int strobogrammaticInRange(String low, String high) {
108+
int a = low.length(), b = high.length();
109+
long l = Long.parseLong(low), r = Long.parseLong(high);
110+
int ans = 0;
111+
for (n = a; n <= b; ++n) {
112+
for (String s : dfs(n)) {
113+
long v = Long.parseLong(s);
114+
if (l <= v && v <= r) {
115+
++ans;
116+
}
117+
}
118+
}
119+
return ans;
120+
}
121+
122+
private List<String> dfs(int u) {
123+
if (u == 0) {
124+
return Collections.singletonList("");
125+
}
126+
if (u == 1) {
127+
return Arrays.asList("0", "1", "8");
128+
}
129+
List<String> ans = new ArrayList<>();
130+
for (String v : dfs(u - 2)) {
131+
for (var p : PAIRS) {
132+
ans.add(p[0] + v + p[1]);
133+
}
134+
if (u != n) {
135+
ans.add(0 + v + 0);
136+
}
137+
}
138+
return ans;
139+
}
140+
}
141+
```
142+
143+
### **C++**
144+
145+
```cpp
146+
using ll = long long;
147+
148+
class Solution {
149+
public:
150+
const vector<pair<char, char>> pairs = {{'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'}};
151+
152+
int strobogrammaticInRange(string low, string high) {
153+
int n;
154+
function<vector<string>(int)> dfs = [&](int u) {
155+
if (u == 0) return vector<string>{""};
156+
if (u == 1) return vector<string>{"0", "1", "8"};
157+
vector<string> ans;
158+
for (auto& v : dfs(u - 2)) {
159+
for (auto& [l, r] : pairs) ans.push_back(l + v + r);
160+
if (u != n) ans.push_back('0' + v + '0');
161+
}
162+
return ans;
163+
};
164+
165+
int a = low.size(), b = high.size();
166+
int ans = 0;
167+
ll l = stoll(low), r = stoll(high);
168+
for (n = a; n <= b; ++n) {
169+
for (auto& s : dfs(n)) {
170+
ll v = stoll(s);
171+
if (l <= v && v <= r) {
172+
++ans;
173+
}
174+
}
175+
}
176+
return ans;
177+
}
178+
};
179+
```
61180

181+
### **Go**
182+
183+
```go
184+
func strobogrammaticInRange(low string, high string) int {
185+
n := 0
186+
var dfs func(int) []string
187+
dfs = func(u int) []string {
188+
if u == 0 {
189+
return []string{""}
190+
}
191+
if u == 1 {
192+
return []string{"0", "1", "8"}
193+
}
194+
var ans []string
195+
pairs := [][]string{{"1", "1"}, {"8", "8"}, {"6", "9"}, {"9", "6"}}
196+
for _, v := range dfs(u - 2) {
197+
for _, p := range pairs {
198+
ans = append(ans, p[0]+v+p[1])
199+
}
200+
if u != n {
201+
ans = append(ans, "0"+v+"0")
202+
}
203+
}
204+
return ans
205+
}
206+
a, b := len(low), len(high)
207+
l, _ := strconv.Atoi(low)
208+
r, _ := strconv.Atoi(high)
209+
ans := 0
210+
for n = a; n <= b; n++ {
211+
for _, s := range dfs(n) {
212+
v, _ := strconv.Atoi(s)
213+
if l <= v && v <= r {
214+
ans++
215+
}
216+
}
217+
}
218+
return ans
219+
}
62220
```
63221

64222
### **...**

solution/0200-0299/0248.Strobogrammatic Number III/README_EN.md

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,151 @@
3333
### **Python3**
3434

3535
```python
36-
36+
class Solution:
37+
def strobogrammaticInRange(self, low: str, high: str) -> int:
38+
def dfs(u):
39+
if u == 0:
40+
return ['']
41+
if u == 1:
42+
return ['0', '1', '8']
43+
ans = []
44+
for v in dfs(u - 2):
45+
for l, r in ('11', '88', '69', '96'):
46+
ans.append(l + v + r)
47+
if u != n:
48+
ans.append('0' + v + '0')
49+
return ans
50+
51+
a, b = len(low), len(high)
52+
low, high = int(low), int(high)
53+
ans = 0
54+
for n in range(a, b + 1):
55+
for s in dfs(n):
56+
if low <= int(s) <= high:
57+
ans += 1
58+
return ans
3759
```
3860

3961
### **Java**
4062

4163
```java
64+
class Solution {
65+
private static final int[][] PAIRS = {{1, 1}, {8, 8}, {6, 9}, {9, 6}};
66+
private int n;
67+
68+
public int strobogrammaticInRange(String low, String high) {
69+
int a = low.length(), b = high.length();
70+
long l = Long.parseLong(low), r = Long.parseLong(high);
71+
int ans = 0;
72+
for (n = a; n <= b; ++n) {
73+
for (String s : dfs(n)) {
74+
long v = Long.parseLong(s);
75+
if (l <= v && v <= r) {
76+
++ans;
77+
}
78+
}
79+
}
80+
return ans;
81+
}
82+
83+
private List<String> dfs(int u) {
84+
if (u == 0) {
85+
return Collections.singletonList("");
86+
}
87+
if (u == 1) {
88+
return Arrays.asList("0", "1", "8");
89+
}
90+
List<String> ans = new ArrayList<>();
91+
for (String v : dfs(u - 2)) {
92+
for (var p : PAIRS) {
93+
ans.add(p[0] + v + p[1]);
94+
}
95+
if (u != n) {
96+
ans.add(0 + v + 0);
97+
}
98+
}
99+
return ans;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
using ll = long long;
108+
109+
class Solution {
110+
public:
111+
const vector<pair<char, char>> pairs = {{'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'}};
112+
113+
int strobogrammaticInRange(string low, string high) {
114+
int n;
115+
function<vector<string>(int)> dfs = [&](int u) {
116+
if (u == 0) return vector<string>{""};
117+
if (u == 1) return vector<string>{"0", "1", "8"};
118+
vector<string> ans;
119+
for (auto& v : dfs(u - 2)) {
120+
for (auto& [l, r] : pairs) ans.push_back(l + v + r);
121+
if (u != n) ans.push_back('0' + v + '0');
122+
}
123+
return ans;
124+
};
125+
126+
int a = low.size(), b = high.size();
127+
int ans = 0;
128+
ll l = stoll(low), r = stoll(high);
129+
for (n = a; n <= b; ++n) {
130+
for (auto& s : dfs(n)) {
131+
ll v = stoll(s);
132+
if (l <= v && v <= r) {
133+
++ans;
134+
}
135+
}
136+
}
137+
return ans;
138+
}
139+
};
140+
```
42141

142+
### **Go**
143+
144+
```go
145+
func strobogrammaticInRange(low string, high string) int {
146+
n := 0
147+
var dfs func(int) []string
148+
dfs = func(u int) []string {
149+
if u == 0 {
150+
return []string{""}
151+
}
152+
if u == 1 {
153+
return []string{"0", "1", "8"}
154+
}
155+
var ans []string
156+
pairs := [][]string{{"1", "1"}, {"8", "8"}, {"6", "9"}, {"9", "6"}}
157+
for _, v := range dfs(u - 2) {
158+
for _, p := range pairs {
159+
ans = append(ans, p[0]+v+p[1])
160+
}
161+
if u != n {
162+
ans = append(ans, "0"+v+"0")
163+
}
164+
}
165+
return ans
166+
}
167+
a, b := len(low), len(high)
168+
l, _ := strconv.Atoi(low)
169+
r, _ := strconv.Atoi(high)
170+
ans := 0
171+
for n = a; n <= b; n++ {
172+
for _, s := range dfs(n) {
173+
v, _ := strconv.Atoi(s)
174+
if l <= v && v <= r {
175+
ans++
176+
}
177+
}
178+
}
179+
return ans
180+
}
43181
```
44182

45183
### **...**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using ll = long long;
2+
3+
class Solution {
4+
public:
5+
const vector<pair<char, char>> pairs = {{'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'}};
6+
7+
int strobogrammaticInRange(string low, string high) {
8+
int n;
9+
function<vector<string>(int)> dfs = [&](int u) {
10+
if (u == 0) return vector<string>{""};
11+
if (u == 1) return vector<string>{"0", "1", "8"};
12+
vector<string> ans;
13+
for (auto& v : dfs(u - 2)) {
14+
for (auto& [l, r] : pairs) ans.push_back(l + v + r);
15+
if (u != n) ans.push_back('0' + v + '0');
16+
}
17+
return ans;
18+
};
19+
20+
int a = low.size(), b = high.size();
21+
int ans = 0;
22+
ll l = stoll(low), r = stoll(high);
23+
for (n = a; n <= b; ++n) {
24+
for (auto& s : dfs(n)) {
25+
ll v = stoll(s);
26+
if (l <= v && v <= r) {
27+
++ans;
28+
}
29+
}
30+
}
31+
return ans;
32+
}
33+
};

0 commit comments

Comments
 (0)