zoukankan      html  css  js  c++  java
  • [LeetCode] Strobogrammatic Number

    Problem Description:

    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.


    The following is the C++ implementation of the suggested solution using a look-up table (implemented as an unordered_map). It takes 0 ms. But, I wonder, are there any real applications of strobogrammatic numbers?

     1 class Solution {
     2 public:
     3     bool isStrobogrammatic(string num) {
     4         make_lut();
     5         int n = num.length(); 
     6         for (int l = 0, r = n - 1; l <= r; l++, r--)
     7             if (lut.find(num[l]) == lut.end() || lut[num[l]] != num[r])
     8                 return false;
     9         return true;
    10     }
    11 private:
    12     unordered_map<char, char> lut; 
    13     void make_lut(void) {
    14         lut['0'] = '0';
    15         lut['1'] = '1';
    16         lut['6'] = '9';
    17         lut['8'] = '8';
    18         lut['9'] = '6';
    19     }
    20 };
  • 相关阅读:
    Tarjan 算法 自学整理
    POJ 2395 Out of Hay
    Codevs 1557 热浪
    Codevs 2956 排队问题
    Codevs 1005 生日礼物
    集合
    奇怪的函数
    关押罪犯
    搭积木
    大数据
  • 原文地址:https://www.cnblogs.com/jcliBlogger/p/4708243.html
Copyright © 2011-2022 走看看