[LeetCode] Strobogrammatic Number

本文介绍了一种使用C++编程语言实现的旋转对称数字检查算法,该算法通过构建查找表来判断给定数字字符串是否为旋转对称数。详细解释了查找表的构建过程,并提供了代码实现,同时探讨了旋转对称数字的实际应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

转载于:https://www.cnblogs.com/jcliBlogger/p/4708243.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值