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.
题解:虽是简单题。但一些case也要留意。
class Solution {
public:
bool isStrobogrammatic(string num) {
int len=num.size();
if(len==0) return true;
int l=0,r=len-1;
while(l<=r) {
if(l==r) {
if(num[l]=='0'||num[l]=='1'||num[l]=='8') return true;
else return false;
}
if(num[l]=='1') {
if(num[r]!='1') return false;
}
else if(num[l]=='6') {
if(num[r]!='9') return false;
}
else if(num[l]=='9') {
if(num[r]!='6') return false;
}
else if(num[l]=='8') {
if(num[r]!='8') return false;
}
else if(num[l]=='0') {
if(num[r]!='0') return false;
}
else return false;
l++;r--;
}
return true;
}
};