Determine whether an integer is a palindrome. Do this without extra space.
题目要求判断一个数是否是回文数,例如123321,9889就是回文数
这道题要求不能使用额外的空间,想了很久都不知道怎么做,只有网上查了下,原来创建一个变量不叫使用额外空间。。。
public class Solution {
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
}
int length = 1;
while (x / length >= 10) {
length = length * 10;
}
while (x > 0) {
int l = x / length;
int r = x % 10;
if (l != r) {
return false;
}
x = (x % length) / 10; //去掉头和尾
length = length / 100;
}
return true;
}
}