判断数字是否为回文数字,首先对位数进行判断,拿到位数值之后对第一位数字和最后一位数字进行相等性的判断,由于每次都去掉两位数字所以除以100,解决代码如下:
public class Solution {
public boolean isPalindrome(int x) {
if (x < 0)
return false;
if (x == 0)
return true;
int base = 1;
while (x / base >= 10)
base *= 10;
while (x != 0) {
int leftDigit = x / base;
int rightDigit = x % 10;
if (leftDigit != rightDigit)
return false;
x -= base * leftDigit;
base /= 100;
x /= 10;
}
return true;
}
}