判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
code
class Solution {
public:
bool isPalindrome(int x) {
/*string s = to_string(x);
int len = s.length();
for(int i=0;i<(len+1)/2;i++){
if(s[i]!=s[len-i-1])
return false;
}
return true;*/
if(x<0)
return false;
if(x<10)
return true;
double s = x;
int len = 0;
while(s>1){
s/=10;
++len;
}
int x1=x;
int x2=x;
for(int i=0;i<(len+1)/2;i++){
int a = pow(10,len-i-1);
if(x1/a!=x2%10)
return false;
x1 = x1%a;
x2 = x2/10;
}
return true;
}
};