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. Kotlin异常与Java异常的区别及注解详解

    Kotlin异常与Java异常的区别: throw的Kotlin中是个表达式,这样我们可以将throw作为Elvis表达式[val test = aa ?: bb,这样的则为Elvis表达式,表示如果 ...

  2. dockerhub下载加速

    curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f5dad4ec.m.daocloud.io syste ...

  3. DT系统研究之-自定义新建函数

    说说在destoon中,我们二次开发时新建的函数应该放哪里好? 发现部分同学,在学习研究destoon过程中,新建的一些php函数直接放在模块里面,须知这样放置的话,会产生些不良后果. 首先,新建的该 ...

  4. Python语言程序设计(3)--字符串类型及操作--time库进度条

    1.字符串类型的表示: 三引号可做注释,注释其实也是字符串 2.字符串的操作符 3.字符串处理函数 输出:

  5. idea去除mybatis的xml那个恶心的绿色背景

    https://my.oschina.net/qiudaozhang/blog/2877536

  6. Browsersync 省时浏览器同步测试工具,浏览器自动刷新,多终端同步

    官网地址 http://www.browsersync.cn/ 1.安装 BrowserSync npm install -g browser-sync 2.启动 BrowserSync // --f ...

  7. Stirling数入门

    第一类Stirling数 定义 $$\begin{aligned}(x)_n & =x(x-1)...(x-n+1)\\&= s(n, 0) + s(n,1)x +..+s(n,n)x ...

  8. make 命令出现:"make:*** No targets specified and no makefile found.Stop."

    我们在Linux 安装包的时候,使用make 命令出现:"make:*** No targets specified and no makefile found.Stop."这样的 ...

  9. vue解决大文件断点续传

    一.概述 所谓断点续传,其实只是指下载,也就是要从文件已经下载的地方开始继续下载.在以前版本的HTTP协议是不支持断点的,HTTP/1.1开始就支持了.一般断点下载时才用到Range和Content- ...

  10. /etc/rc.local

    /etc/rc.local是/etc/rc.d/rc.local的软连接 应用于指定开机启动的进程 开机启动不生效,则首先需要检查下/etc/rc.d/rc.local是否具有可执行权限 在配置文件中 ...