05 August 2008
判断是否是中心对称数,旋转 180 度(倒置)时看起来相同的数字。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
public boolean isStrobogrammatic(String num) {
/*
Thought process for simulation:
What characters can be reversable?
0 -> Yes
1 -> Yes
2 -> No
3 -> No
4 -> No
5 -> No
6 -> Yes*
7 -> No
8 -> Yes
9 -> Yes*
For 0, 1, 8 we can simply add to string,
if see do 6, 9 then we just switch those.
Final piece is to reverse string (using stringbuilder insert in 0th position)
*/
List<Character> exludeList = Arrays.asList('2', '3', '4', '5', '7');
StringBuilder strobeString = new StringBuilder();
for(char c : num.toCharArray()) {
if(exludeList.contains(c)) {
return false;
}
if(c == '9') {
strobeString.insert(0, 6);
} else if (c == '6') {
strobeString.insert(0, 9);
} else {
strobeString.insert(0, c);
}
}
return strobeString.toString().equals(num);
}
}