9. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.
Some hints:
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
Subscribe to see which companies asked this question.
public boolean isPalindrome(int x) {
if(x<0){
return false;
}
if(x<10){
return true;
}
if(x%10==0){
return false;
}
String s = x+"";
char[] c = s.toCharArray();
int max = c.length-1;
int min = 0;
int length = c.length/2;
while(max>=min){
if(c[max]!=c[min]){
return false;
}
max--;
min++;
}
return true;
}
本文探讨了如何在不使用额外空间的情况下确定一个整数是否为回文数。提供了算法思路及实现代码,并讨论了负数、整数反转时可能发生的溢出等问题。
385

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



