?LeetCode刷題實戰(zhàn)246:中心對稱數(shù)
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;
}
}
