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


题目地址: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 日 —— 举杯邀明月,对影成三人

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值