[leetcode] 246. Strobogrammatic Number 解题报告

题目链接:https://leetcode.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.

For example, the numbers "69", "88", and "818" are all strobogrammatic.


思路:用hash表来存储并构造一个旋转后的字符串,然后比较是否相等.

代码如下:

class Solution {
public:
    bool isStrobogrammatic(string num) {
        if(num.size()==0) return true;
        unordered_map<char, char> hash{{'0','0'},{'1','1'}, {'2','#'},{'3','#'},
            {'4','#'},{'5','#'},{'6','9'},{'7','#'},{'8','8'},{'9','6'}};
        string result;
        for(int i = num.size()-1; i >=0; i--)
            result += hash[num[i]];
        return num == result;
    }
};


你可能感兴趣的:(LeetCode,String,hash)