[LeetCode] Strobogrammatic Number

246. Strobogrammatic Number

Given a string num which represents an integer, return true if num is a strobogrammatic number.

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
bool isStrobogrammatic(string num) {
int start = 0, end = num.size() - 1, mid = num.size() / 2;
for(; start <= mid; start++, end--) {
switch (num[start]) {
case '0' :
case '1' :
case '8' :
if(num[end] != num[start]) return false;
break;
case '6' :
if(num[end] != '9') return false;
break;
case '9' :
if(num[end] != '6') return false;
break;
default:
return false;
}
}
return true;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/03/08/PS/LeetCode/strobogrammatic-number/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.