5
5
import java .util .Map ;
6
6
import java .util .Set ;
7
7
8
- /**A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
9
-
10
- Write a function to determine if a number is strobogrammatic. The number is represented as a string.
11
-
12
- For example, the numbers "69", "88", and "818" are all strobogrammatic.*/
8
+ /**
9
+ * A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
10
+ * <p>
11
+ * Write a function to determine if a number is strobogrammatic. The number is represented as a string.
12
+ * <p>
13
+ * For example, the numbers "69", "88", and "818" are all strobogrammatic.
14
+ */
13
15
14
16
public class _246 {
15
17
16
18
public boolean isStrobogrammatic_map (String num ) {
17
- int i = 0 , j = num .length ()- 1 ;
19
+ int i = 0 , j = num .length () - 1 ;
18
20
Map <Character , Character > map = new HashMap ();
19
21
map .put ('8' , '8' );
20
22
map .put ('1' , '1' );
21
23
map .put ('0' , '0' );
22
- if (j == 0 ) return map .containsKey (num .charAt (i ));
23
-
24
+ if (j == 0 ) return map .containsKey (num .charAt (i ));
25
+
24
26
map .put ('9' , '6' );
25
27
map .put ('6' , '9' );
26
- while (i < j ){
27
- if (!map .containsKey (num .charAt (i )) || !map .containsKey (num .charAt (j ))) return false ;
28
- if (map .get (num .charAt (i )) != num .charAt (j )) return false ;
29
- i ++;j --;
28
+ while (i < j ) {
29
+ if (!map .containsKey (num .charAt (i )) || !map .containsKey (num .charAt (j ))) {
30
+ return false ;
31
+ }
32
+ if (map .get (num .charAt (i )) != num .charAt (j )) {
33
+ return false ;
34
+ }
35
+ i ++;
36
+ j --;
30
37
}
31
38
return map .containsKey (num .charAt (i ));
32
39
}
33
40
34
-
41
+
35
42
public boolean isStrobogrammatic_set (String num ) {
36
43
Set <Character > set = new HashSet ();
37
44
set .add ('0' );
@@ -40,16 +47,17 @@ public boolean isStrobogrammatic_set(String num) {
40
47
set .add ('8' );
41
48
set .add ('9' );
42
49
char [] nums = num .toCharArray ();
43
- int i = 0 , j = num .length ()- 1 ;
44
- while (i <= j ){
45
- if (!set .contains (nums [i ]) || !set .contains (nums [j ])) return false ;
46
- if (nums [i ] == '6' && nums [j ] != '9' ) return false ;
47
- else if (nums [i ] == '9' && nums [j ] != '6' ) return false ;
48
- else if (nums [i ] == '1' && nums [j ] != '1' ) return false ;
49
- else if (nums [i ] == '8' && nums [j ] != '8' ) return false ;
50
- else if (nums [i ] == '0' && nums [j ] != '0' ) return false ;
50
+ int i = 0 , j = num .length () - 1 ;
51
+ while (i <= j ) {
52
+ if (!set .contains (nums [i ]) || !set .contains (nums [j ])) return false ;
53
+ if (nums [i ] == '6' && nums [j ] != '9' ) return false ;
54
+ else if (nums [i ] == '9' && nums [j ] != '6' ) return false ;
55
+ else if (nums [i ] == '1' && nums [j ] != '1' ) return false ;
56
+ else if (nums [i ] == '8' && nums [j ] != '8' ) return false ;
57
+ else if (nums [i ] == '0' && nums [j ] != '0' ) return false ;
51
58
else {
52
- i ++; j --;
59
+ i ++;
60
+ j --;
53
61
}
54
62
}
55
63
return true ;
0 commit comments