[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 };

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM