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: trueExample 2:
Input: "88"
Output: trueExample 3:
Input: "962"
Output: falsebool isStrobogrammatic(string num) { // time: O(n); space: O(1)
string rotate(10, ' ');
rotate['0' - '0'] = '0';
rotate['1' - '0'] = '1';
rotate['6' - '0'] = '9';
rotate['8' - '0'] = '8';
rotate['9' - '0'] = '6';
int l = 0, r = num.length() - 1;
while (l <= r) {
if (rotate[num[l] - '0'] != num[r]) return false;
++l, --r;
}
return true;
}Last updated
Was this helpful?