一,题目
Determine whether an integer is a palindrome. Do this without extra space.
二,思路
和整数翻转一样
注意:负数都不是回文
三,代码
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
}
if (x <= 9) {
return true;
}
int copy = x;
long result = 0;
while (x != 0) {
result = result * 10 + x % 10;
x /= 10;
}
if (result == copy) {
return true;
}
return false;
}
本文介绍了一种不使用额外空间判断整数是否为回文的方法。通过反转整数并与原数比较来实现,特别指出负数不是回文。

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



