Determine whether an integer is a palindrome. Do this without extra space.判断一个数字是否是回文的,刚刚reverse一个整数的方法在这里不能直接使用,因为一个整型reverse之后有可能溢出的!!!(至于为什么要说这句话,聪明的读者应该能跟我有默契吧),但是我们可以使用reverse的思想,首先遍历一个整数的各个位,得到这个数有多少位,然后根据这个位数的奇偶性质分开判断。
class Solution {
public:
bool isPalindrome(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int temp=x;
int count=1;
if(x<0)
return false;
for( ; temp/10!=0 ; temp=temp/10)
count++;
temp=0;
for(int i=0 ; i<count/2 ; i++){
temp=temp*10+x%10;
x=x/10;
}
if(count%2==1)
x=x/10;
if(temp==x)
return true;
return false;
}
};