题目地址:https://leetcode-cn.com/problems/strobogrammatic-number/

题目描述

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 度之后看起来依旧相同的数字(或者上下颠倒地看)。
请写一个函数来判断该数字是否是中心对称数,其输入将会以一个字符串的形式来表达数字。

解题方法

字典

这个题和1056. Confusing Number是类似的,只不过这个题目使用的是字符串,因此需要更小心:字符串如果转整数越界!所以使用一个指针从最右边移动到最左边,把翻转后的字符拼接到一起,判断和原始字符串是否相等。

C++代码如下:

class Solution {
public:
bool isStrobogrammatic(string num) {
if (num.empty()) return false;
unordered_map<char, char> m{{'0', '0'}, {'1', '1'}, {'6', '9'}, {'8', '8'}, {'9', '6'}};
string rotate;
int pos = num.size() - 1;
while (pos != -1) {
char mod = num[pos];
if (!m.count(mod))
return false;
rotate += m[mod];
pos --;
}
return rotate == num;
}
};

日期

2019 年 9 月 19 日 —— 举杯邀明月,对影成三人

【LeetCode】246. Strobogrammatic Number 解题报告(C++)的更多相关文章

  1. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  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 (可颠倒数字) $

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

  4. LeetCode 246. Strobogrammatic Number

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

  5. LeetCode 509 Fibonacci Number 解题报告

    题目要求 The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, su ...

  6. LeetCode 136 Single Number 解题报告

    题目要求 Given a non-empty array of integers, every element appears twice except for one. Find that sing ...

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

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

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

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

  9. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

随机推荐

  1. MYSQL5.8----2

    一定要按照这个顺序,where group by having order by limit 可以进行一次排序之后再一次拍寻 #存储的时候,能存数字就村数字

  2. du命令之计算文件大小

    在linux中,常用du命令来计算文件或目录的大小 名称: du - 计算每个文件的磁盘用量,目录则取总用量. 用法: du [选项]... [文件]... 常用选项 -a, --all 输出所有文件 ...

  3. 突破冯·诺依曼架构瓶颈!全球首款存算一体AI芯片诞生

    过去70年,计算机一直遵循冯·诺依曼架构设计,运行时数据需要在处理器和内存之间来回传输. 随着时代发展,这一工作模式面临较大挑战:在人工智能等高并发计算场景中,数据来回传输会产生巨大的功耗:目前内存系 ...

  4. 学习java的第十二天

    一.今日收获(前两天家里有事,博客都忘了发了,唉) 1.通过看哔哩哔哩看黑马程序员的教学视频,学习了java中的数据类型自动转换.强制转换及注意事项三节 2.简单看了看完全学习手册 二.今日问题 1. ...

  5. 日常Java 2021/10/17

    今天开始Javaweb编译环境调试,从tomcat容器开始,然后mysql的下载,连接工具datagrip,navicat for mysql,然后就是编写自己的sql,安装jdbc,eclipse连 ...

  6. Sharding-JDBC 简介

    什么是Sharding-JDBC 1.是轻量级的 java 框架,是增强版的 JDBC 驱动2. Sharding-JDBC(1)主要目的是:简化对分库分表之后数据相关操作.不是帮我们做分库分表,而是 ...

  7. vue-cli4脚手架搭建一

    涉及内容 html  css   javascript   node.js   npm    webpack 2.9.6是常用版本 vue-cli4是基于webpack的 webpack是基于node ...

  8. 【编程思想】【设计模式】【结构模式Structural】组合模式composite

    Python版 https://github.com/faif/python-patterns/blob/master/structural/composite.py #!/usr/bin/env p ...

  9. 【编程思想】【设计模式】【行为模式Behavioral】策略模式strategy

    Python版 转自https://github.com/faif/python-patterns/blob/master/behavioral/strategy.py #!/usr/bin/env ...

  10. 使用jquery刷新页面以及javascript的一些基本函数

    如何使用jquery刷新当前页面 下面介绍全页面刷新方法:有时候可能会用到 1.window.location.reload()刷新当前页面. 2.parent.location.reload()刷新 ...