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 a = 1;
int b = x;
int c = 1;
while(b>=10){
a=a*10;
b=b/10;
}
while(a>c){
if((x/a)%10!=(x/c)%10){
return false;
}
a=a/10;
c=c*10;
}
return true;
}
}这个题目主要是把情况考虑全面:
1.负数不算回文字
2.从数字左右两端依次前进找字符比较
本文介绍了一种不使用额外空间判断整数是否为回文数的方法。通过对比整数的首位数字逐步验证其回文特性,特别注意负数不视为有效回文。
675

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



