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;
}
}
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;
}
}
相关文章推荐
- Window
- android studio ndk 环境搭建
- YL杯超级篮球赛
- 源码推荐(0708B):拼图游戏( 闲暇之余用swift写的100多行代码的小游戏 ),UICollectionView
- Android: 亲测解决模拟器启动慢的问题
- windows程序设计学习笔记-设备无关位图
- 职业素养与职业声誉——北漂18年(62)
- Eclipse开发Android程序如何在手机上运行
- PCL1.8.0,VS2013配置教程。
- Delete Volume 操作 - 每天5分钟玩转 OpenStack(57)
- Delete Volume 操作 - 每天5分钟玩转 OpenStack(57)
- 1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
- Delete Volume 操作 - 每天5分钟玩转 OpenStack(57)
- 第39课:spark streaming中direct kafka 内幕实现彻底解密
- 欢迎使用CSDN-markdown编辑器
- 浅谈图标布局
- java学习之六(IO流上篇)
- Mybatis属性和数据库表字段不一致处理
- 15.4 DS1302的硬件信息
- 15.3 实时时钟芯片DS1302介绍