File tree 3 files changed +71
-4
lines changed
solution/0200-0299/0247.Strobogrammatic Number II
3 files changed +71
-4
lines changed Original file line number Diff line number Diff line change 4
4
5
5
## 题目描述
6
6
<!-- 这里写题目描述 -->
7
- None
7
+ <p >中心对称数是指一个数字在旋转了  ; 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 >
8
16
9
17
10
18
## 解法
32
40
33
41
```
34
42
35
- <!-- tabs:end -->
43
+ <!-- tabs:end -->
Original file line number Diff line number Diff line change 3
3
[ 中文文档] ( /solution/0200-0299/0247.Strobogrammatic%20Number%20II/README.md )
4
4
5
5
## 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 >
7
15
8
16
9
17
## Solutions
28
36
29
37
```
30
38
31
- <!-- tabs:end -->
39
+ <!-- tabs:end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments