Problem Description:
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.
Problem Solving Ideas:
The first is to determine whether a negative number, if negative, directly return false. Otherwise proceed to the following judgment:
Start at both ends to the middle of the readings, respectively judge can.
The code is as follows:
1 Public classSolution {2 Public BooleanIspalindrome (intx) {3 intDivisor = 1;4 intLeft ;5 intRight ;6 inttemp =x;7 if(X < 0)8 return false;9 while(Temp/10 > 0) {TenDivisor *= 10; Onetemp = TEMP/10; A } - - while(x! = 0) { theleft = x/Divisor; -right = x% 10; - if(Left! =Right ) - return false; +x = (x% divisor)/10; -divisor = divisor/100; + } A return true; at } -}
Java [Leetcode 9] palindrome number