Leetcode 246. Strobogrammatic Number (Easy) (cpp)

来源:互联网 发布:怎样优化limit 分页 编辑:程序博客网 时间:2024/05/22 12:09

Leetcode 246. Strobogrammatic Number (Easy) (cpp)

Tag: Hash Table, Math

Difficulty: Easy

这是一道locked题目,给评论个“赞”呗?


/*246. Strobogrammatic Number (Easy)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.*/class Solution {public:    bool isStrobogrammatic(string num) {        if (num.empty()) {            return false;        }        unordered_map <char, char> mapping = {{'0', '0'}, {'1', '1'}, { '6', '9'}, {'8', '8'}, {'9', '6'}};        int i = 0, j = num.size() - 1;        while (i <= j) {            if (mapping[num[i]] != num[j]) {                return false;            }            i++;            j--;        }        return true;    }};


0 0
原创粉丝点击