leetcode#246 中心对称数
题目:
中心对称数是指一个数字在旋转了 180 度之后看起来依旧相同的数字(或者上下颠倒地看)。
请写一个函数来判断该数字是否是中心对称数,其输入将会以一个字符串的形式来表达数字。
示例:
输入: num = "69"
输出: true
思路:
可以想到,只有6,9,1,8,0关于中心对称,那么我们可以把它们替换成他们对称的数,然后变换前后的字符串是否对称就可以了。
代码:
class Solution
{
public:
bool isStrobogrammatic(string num)
{
unordered_map<char, char> dic = {{'0', '0'}, {'1', '1'}, {'6', '9'}, {'8', '8'}, {'9', '6'}};
string tmp;
for(auto i:num)
{
if(dic.count(i))
tmp += dic[i];
else
return false;
}
reverse(tmp.begin(), tmp.end());
return tmp == num;
}
};