Determine whether an integer is a palindrome. Do this without extra space.
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.
public class Solution { public boolean isPalindrome(int x) { if(x<0)return false; int[] str = new int[11]; int index=0; while(x>0){ str[index++]=x%10; x/=10; } int i=0,j=index-1; while(i<j){ if(str[i]!=str[j])return false; i++;j--; } return true; } }
bool isPalindrome(int x) { int i,j,index=0; char str[12]; if(x<0)return false; while(x>0){ str[index++]=x%10+'0'; x=x/10; } i=0;j=index-1; while(i<j){ if(str[i]!=str[j])return false; i++;j--; } return true; }
class Solution { public: bool isPalindrome(int x) { if(x<0)return false; char str[11]; int index=0; while(x>0){ str[index++]=x%10; x/=10; } int i=0,j=index-1; while(i<j){ if(str[i]!=str[j])return false; i++;j--; } return true; } };
class Solution: # @param {integer} x # @return {boolean} def isPalindrome(self, x): if x<0:return False str=[] while x>0: str.append(x%10) x/=10 i=0;j=len(str)-1 while i<j: if str[i]!=str[j]:return False i+=1;j-=1; return True
原文地址:http://blog.csdn.net/runningtortoises/article/details/45558385