Determine whether an integer is a palindrome. Do this without extra space.
public class Solution {
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
}
int div = 1;
while (x / div >= 10) {
div *= 10;
}
while (x != 0) {
int left = x / div;
int right = x % 10;
if (left != right) {
return false;
}
x = (x % div) / 10;
div /= 100;
}
return true;
}
}
本文介绍了一种高效判断整数是否为回文数的方法,该方法不使用额外空间,通过算法巧妙地比较整数的首位数字来实现。适用于计算机科学与编程领域的基础知识学习。
365

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



