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.

翻转180度后对称的数有:8->8, 0->0, 1->1, 6->9, 9->6,从两边向中间检查对应位置的两个数是否满足对称数就行了。比如619,先判断6和9是有映射的,然后1和自己又是映射,所以是对称数。有点像判断回文Palindrome,回文判断是否相等,这里判断是否满足那几个数字的条件。判断是可以直接写条件判断,也可以用HashMap存放数字的映射,然后用双指针从两边向中间查看。

Java:

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;
}
}

Python:

class Solution:
lookup = {'0':'0', '1':'1', '6':'9', '8':'8', '9':'6'} def isStrobogrammatic(self, num):
n = len(num)
for i in xrange((n+1) / 2):
if num[n-1-i] not in self.lookup or \
num[i] != self.lookup[num[n-1-i]]:
return False
return True

Python: wo

class Solution():
def strobogrammatic(self, s):
lookup = {'1': '1', '8': '8', '0': '0', '6': '9', '9': '6'}
i, j = 0, len(s) - 1
while i <= j:
if s[i] not in lookup or lookup[s[i]] != s[j]:
return False
i += 1
j -= 1 return True 

C++:

class Solution {
public:
bool isStrobogrammatic(string num) {
unordered_map<char, char> m {{'0', '0'}, {'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'}};
for (int i = 0; i <= num.size() / 2; ++i) {
if (m[num[i]] != num[num.size() - i - 1]) return false;
}
return true;
}
};

 

类似题目: 

[LeetCode] 247. Strobogrammatic Number II 对称数II

[LeetCode] 248. Strobogrammatic Number III 对称数III

All LeetCode Questions List 题目汇总

  

[LeetCode] 246. Strobogrammatic Number 对称数的更多相关文章

  1. [LeetCode] Strobogrammatic Number 对称数

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  2. LeetCode 246. Strobogrammatic Number (可颠倒数字) $

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  3. LeetCode 246. Strobogrammatic Number

    原题链接在这里:https://leetcode.com/problems/strobogrammatic-number/ 题目: A strobogrammatic number is a numb ...

  4. [LeetCode] 247. Strobogrammatic Number II 对称数II

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  5. [LeetCode] 248. Strobogrammatic Number III 对称数III

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  6. [LeetCode] 248. Strobogrammatic Number III 对称数之三

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  7. 246. Strobogrammatic Number 上下对称的数字

    [抄题]: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at u ...

  8. 【LeetCode】246. Strobogrammatic Number 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

  9. 246. Strobogrammatic Number

    题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at ups ...

随机推荐

  1. C# 验证控件组

    C# 验证控件允许使用ValidationGroup给验证控件分组,分组后的两组验证控件可以独立使用,互不相干.比如一个页面有登录和注册两个部分,假如使用验证控件组,提交的时候会对所有的验证控件进行验 ...

  2. C# 反射(转)

    什么是反射 Reflection,中文翻译为反射.        这是.Net中获取运行时类型信息的方式,.Net的应用程序由几个部分:‘程序集(Assembly)’.‘模块(Module)’.‘类型 ...

  3. Git学习笔记--实践(三)

    文中红色的文字(标为:## 插曲)是在Git学习/实践过程中,我个人遇到的一些问题,每个“## 插曲”之后,都有相应的解决方案. 一.创建版本库 版本库又名仓库,英文名repository,可简单的理 ...

  4. 题解 UVa11489

    题目大意 多组数据,每组数据给定一个整数字串,两个人每次从中抽数,要求每次剩余的数都是 \(3\) 的倍数,请求出谁会获胜. 分析 由于 \(p-1\) 的因数的倍数在 \(p\) 进制下的各位数字之 ...

  5. 03-Flutter移动电商实战-底部导航栏制作

    1.cupertino_IOS风格介绍 在Flutter里是有两种内置风格的: material风格: Material Design 是由 Google 推出的全新设计语言,这种设计语言是为手机.平 ...

  6. 详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud

    转载地址:https://www.jb51.net/article/109382.htm 这篇文章主要介绍了详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud,会通过 N ...

  7. Greenplum常用的gp_toolkit & pg_catalog监控语句

    gp_toolkit 说明 Greenplum数据库提供了一个名为gp_tooikit的管理schema,该schema下有关于查询系统目录,日志文件, 用户创建(databases,schema,t ...

  8. mysql linux上安装使用

    安装启动 安装之前可以看下系统中有没有已经安装. 查看所有软件:dpkg -l 1.查看mysql安装的版本 mysql --version 2.mysql状态 service mysql statu ...

  9. Cogs 1632. 搬运工(二分图最小点覆盖)

    搬运工 ★ 输入文件:worker.in 输出文件:worker.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] 小涵向小宇推荐了一款小游戏. 游戏是这样的,在一个n*n的地 ...

  10. web安全总结

    一.XSS 首先说下最常见的 XSS 漏洞,XSS (Cross Site Script),跨站脚本攻击,因为缩写和 CSS (Cascading Style Sheets) 重叠,所以只能叫 XSS ...