9. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.
Some hints:
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
题目大概就是说的判断一个整型数是不是回文数,负数不可以是回文数,这里我们采用的是求取回文左右两边的数,中间的数可以可以不用管,因为只要两边相等就可以 例如1234321,123321 其实都是回文数啦,我们需要求的也就是123.。。。
代码:
bool isPalindrome(int i) {
if(i<0||i!=0&&i%10==0) return false;
int ret=0;
while(i>ret){
ret=ret*10+i%10;
i=i/10;
}
return (i==ret||i==ret/10);
}