Skip to content

Commit 4923650

Browse files
committed
LeetCode 付费题目 247
1 parent 9510a53 commit 4923650

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44

55
## 题目描述
66
<!-- 这里写题目描述 -->
7-
None
7+
<p>中心对称数是指一个数字在旋转了&nbsp;180 度之后看起来依旧相同的数字(或者上下颠倒地看)。</p>
8+
9+
<p>找到所有长度为 n 的中心对称数。</p>
10+
11+
<p><strong>示例</strong> <strong>:</strong></p>
12+
13+
<pre><strong>输入:</strong> n = 2
14+
<strong>输出:</strong> <code>["11","69","88","96"]</code>
15+
</pre>
816

917

1018
## 解法
@@ -32,4 +40,4 @@ None
3240
3341
```
3442

35-
<!-- tabs:end -->
43+
<!-- tabs:end -->

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
[中文文档](/solution/0200-0299/0247.Strobogrammatic%20Number%20II/README.md)
44

55
## Description
6-
None
6+
<p>A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).</p>
7+
8+
<p>Find all strobogrammatic numbers that are of length = n.</p>
9+
10+
<p><b>Example:</b></p>
11+
12+
<pre><b>Input:</b> n = 2
13+
<b>Output:</b> <code>["11","69","88","96"]</code>
14+
</pre>
715

816

917
## Solutions
@@ -28,4 +36,4 @@ None
2836
2937
```
3038

31-
<!-- tabs:end -->
39+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
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+
}
10+
11+
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;
18+
}
19+
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;
24+
}
25+
26+
for (char c : map.keySet()) {
27+
int num = c - '0';
28+
// 首位不能是0
29+
if (tmp.length() == 0 && num == 0) {
30+
continue;
31+
}
32+
// 奇数的中间位只能是 0 1 8
33+
if (n % 2 != 0 && tmp.length() == n / 2 && !(num == 0 || num == 1 || num == 8)) {
34+
continue;
35+
}
36+
dfs(n, ans, tmp + num);
37+
}
38+
}
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+
}

0 commit comments

Comments
 (0)