回文数字,即77、242、3993、12321之类的数字称之为回文数字
题目的难点在于不允许使用额外的空间,只能将数字的首位挨个比较。
java代码如下
public class Solution {
public boolean isPalindrome(int x) {
if(x < 0)
{
return false;
}
else
{
int len = 0, num = x;
int leftIndex = 0, rightIndex = 1, left, right;
while(num > 0)
{
num = num / 10;
len++;
if(leftIndex == 0)
{
leftIndex = 1;
}
else
{
leftIndex *= 10;
}
}
num = x;
for(int i = 0; i < len / 2; i++)
{
left = (num / leftIndex) % 10;
right = (num / rightIndex) % 10;
leftIndex /= 10;
rightIndex *= 10;
if(left != right)
{
return false;
}
}
return true;
}
}
}