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: num = "69"
Output: true
Example 2:
Input: num = "88"
Output: true
Example 3:
Input: num = "962"
Output: false
Example 4:
Input: num = "1"
Output: true
我的答案:一开始忘了0
class Solution {
public:
bool isStrobogrammatic(string num) {
int left = 0;
int right = num.size()-1;
while (left <= right) { //check
if (
(num[left] == '6' and num[right] == '9') or
(num[left] == '9' and num[right] == '6') or
(num[left] == '8' and num[right] == '8') or
(num[left] == '1' and num[right] == '1') or
(num[left] == '0' and num[right] == '0')
) {
++left;
--right;
}
else {
return false;
}
}
return true;
}
};
Runtime: 4 ms, faster than 100.00% of C++ online submissions for Strobogrammatic Number.
Memory Usage: 6.4 MB, less than 30.62% of C++ online submissions for Strobogrammatic Number.