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.
public class Solution {
public boolean isStrobogrammatic(String num) {
int i=0;
int l=num.length() -1;
while(i<=l){
if(num.charAt(i) == '0'){
if(num.charAt(l) != '0'){
return false;
}
}
else if(num.charAt(i) == '1'){
if(num.charAt(l) != '1'){
return false;
}
}
else if(num.charAt(i) == '8'){
if(num.charAt(l) != '8'){
return false;
}
}
else if(num.charAt(i) == '6'){
if(num.charAt(l) != '9'){
return false;
}
}
else if(num.charAt(i) == '9'){
if(num.charAt(l) != '6'){
return false;
}
}
else{
return false;
}
i++;
l--;
}
return true;
}
}
微信扫一扫
支付宝扫一扫