Determine whether an integer is a palindrome. Do this without extra space.
Some hints:
Could negative integers be palindromes? (ie,-1)
If you is thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you had solved the problem "Reverse integer", you know that the reversed integer might overflow. How do would handle such case?
There is a more generic the solving this problem.
So the key to the problem is to remember to judge the negative, the program is very ugly writing.
1 //2 //main.cpp3 //Longest Substring4 //5 //Created by Bowie Hsu on 29/11/21.6 //Copyright (c) 2014 Bowie Hsu. All rights reserved.7 //8 9#include <iostream>Ten#include <string> One#include <sstream> A#include <vector> -#include"stdio.h" -#include"ctype.h" the - using namespacestd; - - classSolution + { - Public: + BOOLIspalindrome (intx) A { at //record the length of an input integer - if(x<0) { - return 0; - } - intA=1; - intp=0; in intCalc=x; - while(calc/Ten!=0) to { +a=a*Ten; -calc=calc/Ten; the++p; * } $ inttest=A;Panax Notoginseng intBig=x,small=x; - BOOLbign=1; the intCha=0; + while(small) A { the if(big/test!=small%Ten) +bign=0; - $cha=big/test; $big=big-test*cha; -test=test/Ten; -small=small/Ten; the - }Wuyi returnbign; the } - }; Wu - intMain () About { $ intinput=1211; - BOOLans; - solution x; -ans=x.ispalindrome (input); Acout<<ans<<Endl; + //cout<< "What the fuck?"; the}
Palindrome Number Problem Solving report