A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
Write a function to determine if a number is strobogrammatic. The number is represented as a string.
示例
示例 1:
输入: "69"
输出: true
示例 2:
输入: "88"
输出: true
示例 3:
输入: "962"
输出: false
解题
public class Solution {
public boolean isStrobogrammatic(String num) {
HashMap<Character, Character> map = new HashMap<Character, Character>();
map.put('1','1');
map.put('0','0');
map.put('6','9');
map.put('9','6');
map.put('8','8');
int left = 0, right = num.length() - 1;
while(left <= right){
// 如果字母不存在映射或映射不对,则返回假
if(!map.containsKey(num.charAt(right)) || num.charAt(left) != map.get(num.charAt(right))){
return false;
}
left++;
right--;
}
return true;
}
}