File tree Expand file tree Collapse file tree 4 files changed +119
-23
lines changed
solution/0200-0299/0246.Strobogrammatic Number Expand file tree Collapse file tree 4 files changed +119
-23
lines changed Original file line number Diff line number Diff line change 4444<!-- 这里可写当前语言的特殊实现逻辑 -->
4545
4646``` python
47-
47+ class Solution :
48+ def isStrobogrammatic (self , num : str ) -> bool :
49+ def match (a , b ):
50+ if a in {' 0' , ' 1' , ' 8' }:
51+ return a == b
52+ if a == ' 6' :
53+ return b == ' 9'
54+ if a == ' 9' :
55+ return b == ' 6'
56+ return False
57+ n = len (num)
58+ i, j = 0 , n - 1
59+ while i <= j:
60+ if not match(num[i], num[j]):
61+ return False
62+ i += 1
63+ j -= 1
64+ return True
4865```
4966
5067### ** Java**
5168
5269<!-- 这里可写当前语言的特殊实现逻辑 -->
5370
5471``` java
55-
72+ class Solution {
73+ public boolean isStrobogrammatic (String num ) {
74+ int n = num. length();
75+ for (int i = 0 , j = n - 1 ; i <= j; ++ i, -- j) {
76+ if (! match(num. charAt(i), num. charAt(j))) return false ;
77+ }
78+ return true ;
79+ }
80+
81+ private boolean match (char a , char b ) {
82+ switch (a) {
83+ case ' 0' :
84+ case ' 1' :
85+ case ' 8' :
86+ return a == b;
87+ case ' 6' :
88+ return b == ' 9' ;
89+ case ' 9' :
90+ return b == ' 6' ;
91+ default :
92+ return false ;
93+ }
94+ }
95+ }
5696```
5797
5898### ** ...**
Original file line number Diff line number Diff line change 3030### ** Python3**
3131
3232``` python
33-
33+ class Solution :
34+ def isStrobogrammatic (self , num : str ) -> bool :
35+ def match (a , b ):
36+ if a in {' 0' , ' 1' , ' 8' }:
37+ return a == b
38+ if a == ' 6' :
39+ return b == ' 9'
40+ if a == ' 9' :
41+ return b == ' 6'
42+ return False
43+ n = len (num)
44+ i, j = 0 , n - 1
45+ while i <= j:
46+ if not match(num[i], num[j]):
47+ return False
48+ i += 1
49+ j -= 1
50+ return True
3451```
3552
3653### ** Java**
3754
3855``` java
39-
56+ class Solution {
57+ public boolean isStrobogrammatic (String num ) {
58+ int n = num. length();
59+ for (int i = 0 , j = n - 1 ; i <= j; ++ i, -- j) {
60+ if (! match(num. charAt(i), num. charAt(j))) return false ;
61+ }
62+ return true ;
63+ }
64+
65+ private boolean match (char a , char b ) {
66+ switch (a) {
67+ case ' 0' :
68+ case ' 1' :
69+ case ' 8' :
70+ return a == b;
71+ case ' 6' :
72+ return b == ' 9' ;
73+ case ' 9' :
74+ return b == ' 6' ;
75+ default :
76+ return false ;
77+ }
78+ }
79+ }
4080```
4181
4282### ** ...**
Original file line number Diff line number Diff line change 11class Solution {
22 public boolean isStrobogrammatic (String num ) {
3- if (num == null || num .length () == 0 ) {
4- return false ;
3+ int n = num .length ();
4+ for (int i = 0 , j = n - 1 ; i <= j ; ++i , --j ) {
5+ if (!match (num .charAt (i ), num .charAt (j ))) return false ;
56 }
6- Map <Character , Character > map = new HashMap <>();
7- map .put ('0' , '0' );
8- map .put ('1' , '1' );
9- map .put ('6' , '9' );
10- map .put ('9' , '6' );
11- map .put ('8' , '8' );
12- if (num .length () == 1 ) {
13- return "1" .equals (num ) || "8" .equals (num ) || "0" .equals (num );
14- }
15- char [] chars = num .toCharArray ();
16- for (int i = 0 ; i < chars .length ; i ++) {
17- if (!map .containsKey (chars [i ])) {
18- return false ;
19- }
20- if (chars [chars .length - 1 - i ] != map .get (chars [i ])) {
7+ return true ;
8+ }
9+
10+ private boolean match (char a , char b ) {
11+ switch (a ) {
12+ case '0' :
13+ case '1' :
14+ case '8' :
15+ return a == b ;
16+ case '6' :
17+ return b == '9' ;
18+ case '9' :
19+ return b == '6' ;
20+ default :
2121 return false ;
22- }
2322 }
24- return true ;
2523 }
2624}
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def isStrobogrammatic (self , num : str ) -> bool :
3+ def match (a , b ):
4+ if a in {'0' , '1' , '8' }:
5+ return a == b
6+ if a == '6' :
7+ return b == '9'
8+ if a == '9' :
9+ return b == '6'
10+ return False
11+ n = len (num )
12+ i , j = 0 , n - 1
13+ while i <= j :
14+ if not match (num [i ], num [j ]):
15+ return False
16+ i += 1
17+ j -= 1
18+ return True
You can’t perform that action at this time.
0 commit comments