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?
- bool isPalindrome(int x) {
- if(x<0)
- return false;
- int t=x;
- int length=0;
- while(t){
- t=t/10;
- length++;
- }
- for(int i=0;i<length/2;i++){
- if(f(x,i)!=f(x,length-1-i))
- return false;
- }
- return true;
- }
- int f(int x, int n){
- for(int i=0;i<n;i++){
- x=x/10;
- }
- return x%10;
- }
253



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



