Topic:
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.
Analysis: The topic is to determine whether an integer data is a palindrome number, we have seen the string to determine whether a palindrome string, conditioned reflex is the use of the same idea, the integer data into a string. However, it is not allowed to take up extra space, that is to say, we cannot allocate strings to store this number. Another way of thinking, palindrome number is similar to 1, 121, 1221 such data, negative numbers are obviously not palindrome. In other words, we can determine the same from the highest and lowest digits in two directions in turn. AC Code:
Class Solution {public: bool Ispalindrome (int x) {if (x < 0) return false;int size = 0, TMP = x;while (tmp! = 0) {tmp /= 10;size++;} Whileint p = x, q = x, I, J;for (I=1, j=size; i<j; i++, j--) {int a = POW (j-1); if (p/a! = q%10) {return false; }else{p%= a;q/= 10;} Else}//forreturn true; }};
Leetcode (9) palindrome number