首先确定 负数不是回文串
然后判断数字的第一项和最后一项是否相同 不同就返回false. 相同再继续遍历
public class Solution {
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
}
int div = 1;
while (x/10 >= div) {
div *= 10;
}
while (x != 0) {
int left = x/div;
int right = x%10;
if (left != right) {
return false;
}
x = (x%div) / 10;// x变为去掉left & right 的数字 12321--> 232
div /= 100;//因为去掉了left和right 缩小100倍 div也要缩小
}
return true;
}
}
没有评论:
发表评论