55import java .util .Map ;
66import java .util .Set ;
77
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+ */
1315
1416public class _246 {
1517
1618 public boolean isStrobogrammatic_map (String num ) {
17- int i = 0 , j = num .length ()- 1 ;
19+ int i = 0 , j = num .length () - 1 ;
1820 Map <Character , Character > map = new HashMap ();
1921 map .put ('8' , '8' );
2022 map .put ('1' , '1' );
2123 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+
2426 map .put ('9' , '6' );
2527 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 --;
3037 }
3138 return map .containsKey (num .charAt (i ));
3239 }
3340
34-
41+
3542 public boolean isStrobogrammatic_set (String num ) {
3643 Set <Character > set = new HashSet ();
3744 set .add ('0' );
@@ -40,16 +47,17 @@ public boolean isStrobogrammatic_set(String num) {
4047 set .add ('8' );
4148 set .add ('9' );
4249 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 ;
5158 else {
52- i ++; j --;
59+ i ++;
60+ j --;
5361 }
5462 }
5563 return true ;
0 commit comments