码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Strobogrammatic Number

时间:2015-08-06 16:40:04      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

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.find(num[r]) == 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 };

 

[LeetCode] Strobogrammatic Number

标签:

原文地址:http://www.cnblogs.com/jcliBlogger/p/4708243.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!