|
6 | 6 | importjava.util.Set;
|
7 | 7 |
|
8 | 8 | /**
|
| 9 | + * 246. Strobogrammatic Number |
| 10 | + * |
9 | 11 | * A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
|
10 |
| - * <p> |
11 | 12 | * Write a function to determine if a number is strobogrammatic. The number is represented as a string.
|
12 |
| - * <p> |
13 | 13 | * For example, the numbers "69", "88", and "818" are all strobogrammatic.
|
14 | 14 | */
|
15 | 15 |
|
16 | 16 | publicclass_246 {
|
17 | 17 |
|
18 |
| -publicbooleanisStrobogrammatic_map(Stringnum) { |
19 |
| -inti =0; |
20 |
| -intj =num.length() -1; |
21 |
| -Map<Character,Character>map =newHashMap(); |
22 |
| -map.put('8','8'); |
23 |
| -map.put('1','1'); |
24 |
| -map.put('0','0'); |
25 |
| -if (j ==0) { |
26 |
| -returnmap.containsKey(num.charAt(i)); |
27 |
| - } |
28 |
| - |
29 |
| -map.put('9','6'); |
30 |
| -map.put('6','9'); |
31 |
| -while (i <j) { |
32 |
| -if (!map.containsKey(num.charAt(i)) || !map.containsKey(num.charAt(j))) { |
33 |
| -returnfalse; |
| 18 | +publicstaticclassSolution1 { |
| 19 | +publicbooleanisStrobogrammatic(Stringnum) { |
| 20 | +inti =0; |
| 21 | +intj =num.length() -1; |
| 22 | +Map<Character,Character>map =newHashMap(); |
| 23 | +map.put('8','8'); |
| 24 | +map.put('1','1'); |
| 25 | +map.put('0','0'); |
| 26 | +if (j ==0) { |
| 27 | +returnmap.containsKey(num.charAt(i)); |
34 | 28 | }
|
35 |
| -if (map.get(num.charAt(i)) !=num.charAt(j)) { |
36 |
| -returnfalse; |
| 29 | + |
| 30 | +map.put('9','6'); |
| 31 | +map.put('6','9'); |
| 32 | +while (i <j) { |
| 33 | +if (!map.containsKey(num.charAt(i)) || !map.containsKey(num.charAt(j))) { |
| 34 | +returnfalse; |
| 35 | + } |
| 36 | +if (map.get(num.charAt(i)) !=num.charAt(j)) { |
| 37 | +returnfalse; |
| 38 | + } |
| 39 | +i++; |
| 40 | +j--; |
37 | 41 | }
|
38 |
| -i++; |
39 |
| -j--; |
| 42 | +returnmap.containsKey(num.charAt(i)); |
40 | 43 | }
|
41 |
| -returnmap.containsKey(num.charAt(i)); |
42 | 44 | }
|
43 | 45 |
|
44 | 46 |
|
45 |
| -publicbooleanisStrobogrammatic_set(Stringnum) { |
46 |
| -Set<Character>set =newHashSet(); |
47 |
| -set.add('0'); |
48 |
| -set.add('1'); |
49 |
| -set.add('6'); |
50 |
| -set.add('8'); |
51 |
| -set.add('9'); |
52 |
| -char[]nums =num.toCharArray(); |
53 |
| -inti =0; |
54 |
| -intj =num.length() -1; |
55 |
| -while (i <=j) { |
56 |
| -if (!set.contains(nums[i]) || !set.contains(nums[j])) { |
57 |
| -returnfalse; |
58 |
| - } |
59 |
| -if (nums[i] =='6' &&nums[j] !='9') { |
60 |
| -returnfalse; |
61 |
| - }elseif (nums[i] =='9' &&nums[j] !='6') { |
62 |
| -returnfalse; |
63 |
| - }elseif (nums[i] =='1' &&nums[j] !='1') { |
64 |
| -returnfalse; |
65 |
| - }elseif (nums[i] =='8' &&nums[j] !='8') { |
66 |
| -returnfalse; |
67 |
| - }elseif (nums[i] =='0' &&nums[j] !='0') { |
68 |
| -returnfalse; |
69 |
| - }else { |
70 |
| -i++; |
71 |
| -j--; |
| 47 | +publicstaticclassSolution2 { |
| 48 | +publicbooleanisStrobogrammatic(Stringnum) { |
| 49 | +Set<Character>set =newHashSet(); |
| 50 | +set.add('0'); |
| 51 | +set.add('1'); |
| 52 | +set.add('6'); |
| 53 | +set.add('8'); |
| 54 | +set.add('9'); |
| 55 | +char[]nums =num.toCharArray(); |
| 56 | +inti =0; |
| 57 | +intj =num.length() -1; |
| 58 | +while (i <=j) { |
| 59 | +if (!set.contains(nums[i]) || !set.contains(nums[j])) { |
| 60 | +returnfalse; |
| 61 | + } |
| 62 | +if (nums[i] =='6' &&nums[j] !='9') { |
| 63 | +returnfalse; |
| 64 | + }elseif (nums[i] =='9' &&nums[j] !='6') { |
| 65 | +returnfalse; |
| 66 | + }elseif (nums[i] =='1' &&nums[j] !='1') { |
| 67 | +returnfalse; |
| 68 | + }elseif (nums[i] =='8' &&nums[j] !='8') { |
| 69 | +returnfalse; |
| 70 | + }elseif (nums[i] =='0' &&nums[j] !='0') { |
| 71 | +returnfalse; |
| 72 | + }else { |
| 73 | +i++; |
| 74 | +j--; |
| 75 | + } |
72 | 76 | }
|
| 77 | +returntrue; |
73 | 78 | }
|
74 |
| -returntrue; |
75 | 79 | }
|
76 | 80 | }
|