644 · 镜像数字
描述
一个镜像数字是指一个数字旋转180度以后和原来一样(倒着看)。例如,数字"69",“88”,和"818"都是镜像数字。
写下一个函数来判断是否这个数字是镜像的。数字用字符串来表示。
public class Solution {
/**
* @param num: a string
* @return: true if a number is strobogrammatic or false
*/
public boolean isStrobogrammatic(String num) {
// write your code here
HashMap<Character,Character> map = new HashMap<Character,Character>() ;
map.put('6' , '9') ;
map.put('9' , '6') ;
map.put('0' , '0') ;
map.put('1' , '1') ;
map.put('8' , '8') ;
int left = 0 , right = num.length() -1 ;
while(left < right){
if(map.get(num.charAt(left)) == num.charAt(right)){
left++ ;
right-- ;
}else{
return false ;
}
}
return true ;
}
}