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.

Example 1:

Input:  "69"
Output: true

Example 2:

Input:  "88"
Output: true

Example 3:

Input:  "962"
Output: false

这道题定义了一种对称数,就是说一个数字旋转 180 度和原来一样,也就是倒过来看一样,比如 609,倒过来还是 609 等等,满足这种条件的数字其实没有几个,只有 0,1,8,6,9。这道题其实可以看做求回文数的一种特殊情况,还是用双指针来检测,首尾两个数字如果相等的话,只有它们是 0,1,8 中间的一个才行,如果它们不相等的话,必须一个是6一个是9,或者一个是9一个是6,其他所有情况均返回 false,参见代码如下;

解法一:

class Solution {
public:
bool isStrobogrammatic(string num) {
int l = , r = num.size() - ;
while (l <= r) {
if (num[l] == num[r]) {
if (num[l] != '' && num[l] != '' && num[l] != ''){
return false;
}
} else {
if ((num[l] != '' || num[r] != '') && (num[l] != '' || num[r] != '')) {
return false;
}
}
++l; --r;
}
return true;
}
};

由于满足题意的数字不多,所以可以用 HashMap 来做,把所有符合题意的映射都存入哈希表中,然后双指针扫描,看对应位置的两个数字是否在哈希表里存在映射,若不存在,返回 false,遍历完成返回 true,参见代码如下:

解法二:

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

Github 同步地址:

https://github.com/grandyang/leetcode/issues/246

类似题目:

Strobogrammatic Number II

Strobogrammatic Number III

参考资料:

https://leetcode.com/problems/strobogrammatic-number/

https://leetcode.com/problems/strobogrammatic-number/discuss/67182/Accepted-Java-solution

https://leetcode.com/problems/strobogrammatic-number/discuss/67257/5-lines-concise-and-easy-understand-C%2B%2B-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

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

  1. [LeetCode] 246. Strobogrammatic Number 对称数

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

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

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

  3. [LeetCode] Strobogrammatic Number II 对称数之二

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

  4. LeetCode Strobogrammatic Number II

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

  5. LeetCode Strobogrammatic Number

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

  6. Leetcode: Strobogrammatic Number III

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

  7. [LeetCode] Ugly Number 丑陋数

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  8. [LeetCode] Happy Number 快乐数

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

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

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

随机推荐

  1. 翻唱曲练习:龙珠改主题曲 【Dragon Soul】龙之魂

    首先这是个人翻唱曲: 这个是原版(燃): 伴奏:  翻唱合成为动漫AMV 出镜翻唱: 全民K歌链接: http://kg.qq.com/node/play?s=aYpbMWb6UwoU&g_f ...

  2. CSS学习笔记——包含块 containing block

    以下内容翻译自CSS 2.1官方文档.网址:https://www.w3.org/TR/CSS2/visudet.html#strut 有时,一个元素的盒子的位置和尺寸根据一个确定的矩形计算,这个确定 ...

  3. 在thinkphp中,写的博文标签多对多关系的标签频率统计算法

    常常看到别人的博客里面,或者网站里面有这样随机颜色,但字体大小与标签出现频率有关的标签云,于是自己就想写一个.至于颜色的随机显示,那就很简单了,这里就不列代码. 因为正在学thinkphp,所以数据查 ...

  4. css全局格式化

    /*全局控制*/ body{margin:0;padding:0;font-size:14px;line-height:22px; height:auto; font-family:"微软雅 ...

  5. Maven+Spring+Spring MVC+MyBatis+MySQL,搭建SSM框架环境【转】

    项目建设完成之后的结构: 数据库的表结构如下: 环境建设:搭建Maven环境.Tomcat环境.需要MySql 数据库支持,使用的编程工具Eclipse (这些是前期准备): 开始创建工程: 1.创建 ...

  6. windows 7下React Native环境配置

    React Native 是 Facebook 推出的一个用 Java 语言就能同时编写 ios,android,以及后台的一项技术,它可以做到实时热更新 .FaceBook 也号称这们技术是 “Le ...

  7. js类型检测总结

    类型检测: 类和对象: Call,Apply,Bind

  8. ArcGIS Engine开发之地图导出

    关于地图导出的方法有很多,但是核心技术就那么一点.下面是从项目实战中总结的一部分地图导出的方法:(以全域导出和区域导出为例) 1.由于地图导出用到的函数和方法容易重复,增加了工作量故首先将其进行封装成 ...

  9. CentOS7 修改防火墙,增加外网可以访问的端口号

    CentOS7 修改防火墙,增加外网可以访问的端口号: vim /etc/sysconfig/iptables 增加一条 -A INPUT -p tcp -m state --state NEW -m ...

  10. Appfuse:权限控制

    Appfuse的权限控制依赖于Struts的Menu机制,common下的menu.jsp是对菜单顺序的定义,详细的菜单项和菜单链接及权限再menu-config.xml中控制,如下: <Men ...