题目
Determine whether an integer is a palindrome. Do this without extra space.
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.
代码
public class Solution {
public boolean isPalindrome(int x) {
if(x<0){
return false;
}
else{
if(x==0){
return true;
}
else{
int temp=0;
int temp_x=0;
temp=x;
while(temp/10!=0 || (temp<=9 && temp>0)){
temp_x=temp_x*10;
temp_x+=temp%10;
temp=temp/10;
}
// System.out.print(""+temp_x);
if(x==temp_x){
return true;
}
else{
return false;
}
}
}
}
}
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:http://blog.csdn.net/buptgshengod
******************************************/

本文分享了一种快速解决整数回文问题的方法,通过不使用额外空间来判断一个整数是否为回文数。文章提供了Java代码实现,并讨论了处理负数及反转整数可能导致溢出的情况。

被折叠的 条评论
为什么被折叠?



