[lintcode] problem 644 – strobogrammatic number

A mirror number is a number that looks the same when rotated 180 degrees (looked at upside down).For example, the numbers “69”, “88”, and “818” are all mirror numbers.

Write a function to determine if a number is mirror. The number is represented as a string.

Example

No.1

Input : “69”

Output : true

No.2

Input : “68”

Output : false

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public boolean (String num) {
Map<Character, Character> map = new HashMap<Character, Character>(){{
put('0', '0');
put('1', '1');
put('6', '9');
put('8', '8');
put('9', '6');
}};

int i = 0;
int j = num.length() - 1;

while (i <= j) {
if (!map.containsKey(num.charAt(i)) || map.get(num.charAt(i)) != num.charAt(j))
return false;

i++;
j--;
}

return true;
}