Palindrome number
Determines whether a positive integer is a palindrome. No extra space is allowed.
Analysis
The topic suggests that if you want to convert integers to string type, do not meet the topic does not occupy additional space, in addition
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?
So we can only use the mathematical calculation,/and% to calculate each bit.
classSolution { Public: BOOLIspalindrome (intx) {if(x<0) return false; int Base=1; intLeft ; intj=0, i=0; while(x/Base>=Ten){ Base*=Ten; J++; } Left=Base; while(x/left==x%Ten&&x>Ten) {i++; X%=Left ; X/=Ten; if(left> -) Left/= -; } if(x<Ten&&i>=j/2) return true; Else if(x==0) return true; Else return false; }};
"Summary" 124ms
At first, I couldn't imagine how to get the highest bit, each time divided by 10 too cumbersome to see the other people's request base to get enlightenment.
OJ Practice 3--t9