题目:判断一个数是否是回文数字 回文数字就是正着读和反着读一样,比如12321.
要求:不能使用额外的内存空间
思路:反转数字,然后和反转前的数字对比,相等即返回true
public class Solution {
public boolean isPalindrome(int x) {
//负数肯定不是回文数字
if (x < 0) {
return false;
}
int original = x;
//预防反转之后超过int的最大范围
long res = 0;
while (x != 0) {
res = res * 10 + x % 10;
x = x / 10;
}
if (res == original) {
return true;
}
return false;
}
}