Leetcode 246. Strobogrammatic Number (Easy) (cpp)
Tag: Hash Table, Math
Difficulty: Easy
这是一道locked题目,给评论个“赞”呗?
/*
246. Strobogrammatic Number (Easy)
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.
*/
class Solution {
public:
bool isStrobogrammatic(string num) {
if (num.empty()) {
return false;
}
unordered_map <char, char> mapping = {{'0', '0'}, {'1', '1'}, { '6', '9'}, {'8', '8'}, {'9', '6'}};
int i = 0, j = num.size() - 1;
while (i <= j) {
if (mapping[num[i]] != num[j]) {
return false;
}
i++;
j--;
}
return true;
}
};