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.
分析:
回文数,并且题目要求不能使用额外的空间。
即,不能使用回文串的方法。
class Solution {
public boolean isPalindrome(int x) {
int a = 0 , b = x;
while( b > 0){
a = a * 10 + b % 10;
b = b/10;
}
if(a == x) return true;
else return false;
}
}