您的位置:首页 > 其它

Strobogrammatic Number

2016-07-08 07:19 239 查看
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.

For example, the numbers "69", "88", and "818" are all strobogrammatic.

思路:建立一个mapping的连接,用array足够,不需要hashmap,然后双指针从前往后,从后往前扫,其中-1,则false,如果互为颠倒,false。

public class Solution {
public boolean isStrobogrammatic(String num) {
if(num == null || num.length() ==0) return false;
char[] chars = num.toCharArray();
int[] map = new int[]{0,1,-1,-1,-1,-1,9,-1,8,6};

int i=0; int j=chars.length-1;
while(i<=j){
if(map[chars[i]-'0'] == -1 || map[chars[j]-'0'] == -1){
return false;
}
if(map[chars[i]-'0'] != (chars[j]-'0')) {
return false;
}
i++;
j--;
}
return true;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: