# 246. Strobogrammatic Number

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.

Example 1:

``````Input:  "69"
Output: true
``````

Example 2:

``````Input:  "88"
Output: true
``````

Example 3:

``````Input:  "962"
Output: false
``````

``````class Solution {
public:
bool isStrobogrammatic(string num) {
int l = 0, r = num.size() - 1;
while (l <= r) {
if (num[l] == num[r]) {
if (num[l] != '1' && num[l] != '0' && num[l] != '8'){
return false;
}
} else {
if ((num[l] != '6' || num[r] != '9') && (num[l] != '9' || num[r] != '6')) {
return false;
}
}
++l; --r;
}
return true;
}
};
``````

``````class Solution {
public:
bool isStrobogrammatic(string num) {
unordered_map<char, char> m {{'0', '0'}, {'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'}};
for (int i = 0; i <= num.size() / 2; ++i) {
if (m[num[i]] != num[num.size() - i - 1]) return false;
}
return true;
}
};
``````

Github 同步地址：

https://github.com/grandyang/leetcode/issues/246

https://leetcode.com/problems/strobogrammatic-number/

https://leetcode.com/problems/strobogrammatic-number/discuss/67182/Accepted-Java-solution

https://leetcode.com/problems/strobogrammatic-number/discuss/67257/5-lines-concise-and-easy-understand-C%2B%2B-solution

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation